使用 PHP 创建动态表

发布于 2024-12-11 21:33:04 字数 738 浏览 0 评论 0原文

我正在尝试用 PHP 制作一个动态表。我有一个页面显示数据库中的所有图片。 我需要的表只有 5 列。如果返回的图片超过 5 张,则应创建一个新行,并继续显示其余图片。

有人可以帮忙吗?

代码在这里: 主页中的代码:-

    <table>
    <?php
        $all_pics_rs=get_all_pics();
        while($pic_info=mysql_fetch_array($all_pics_rs)){
        echo "<td><img src='".$pic_info['picture']."' height='300px' width='400px' /></td>";
            } 
?>
</table>

get_all_pics() 函数:

$all_pics_q="SELECT * FROM pics";
        $all_pics_rs=mysql_query($all_pics_q,$connection1);
        if(!$all_pics_rs){
            die("Database query failed: ".mysql_error());
        }
        return $all_pics_rs;

此代码创建一行。我想不出如何获得多行......!!

I'm trying to make a dynamic table with PHP. I have a page which displays all the pictures from a database.
I need the table to be of 5 columns only. If more than 5 pictures are returned, it should create a new row and the displaying of the rest of the pics would continue.

Can anyone please help?

Codes go here:
Code in the main page:-

    <table>
    <?php
        $all_pics_rs=get_all_pics();
        while($pic_info=mysql_fetch_array($all_pics_rs)){
        echo "<td><img src='".$pic_info['picture']."' height='300px' width='400px' /></td>";
            } 
?>
</table>

The get_all_pics() function:

$all_pics_q="SELECT * FROM pics";
        $all_pics_rs=mysql_query($all_pics_q,$connection1);
        if(!$all_pics_rs){
            die("Database query failed: ".mysql_error());
        }
        return $all_pics_rs;

This code is creating a single row. I can't think of how I can get multiple rows ... !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白芷 2024-12-18 21:33:04
$maxcols = 5;
$i = 0;

//Open the table and its first row
echo "<table>";
echo "<tr>";
while ($image = mysql_fetch_assoc($images_rs)) {

    if ($i == $maxcols) {
        $i = 0;
        echo "</tr><tr>";
    }

    echo "<td><img src=\"" . $image['src'] . "\" /></td>";

    $i++;

}

//Add empty <td>'s to even up the amount of cells in a row:
while ($i <= $maxcols) {
    echo "<td> </td>";
    $i++;
}

//Close the table row and the table
echo "</tr>";
echo "</table>";

我还没有测试过,但我的大胆猜测是这样的。只需循环浏览包含图像的数据集,只要您还没有创建 5 个 ,就添加一个。一旦达到 5,关闭该行并创建一个新行。

该脚本应该为您提供如下所示的内容。这显然取决于您拥有多少图像,我假设 5(在 $maxcols 中定义)是您想要连续显示的最大图像数量。

<table>
    <tr>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
    </tr>
    <tr>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
        <td> </td>
        <td> </td>
        <td> <td>
    </tr>
</table>
$maxcols = 5;
$i = 0;

//Open the table and its first row
echo "<table>";
echo "<tr>";
while ($image = mysql_fetch_assoc($images_rs)) {

    if ($i == $maxcols) {
        $i = 0;
        echo "</tr><tr>";
    }

    echo "<td><img src=\"" . $image['src'] . "\" /></td>";

    $i++;

}

//Add empty <td>'s to even up the amount of cells in a row:
while ($i <= $maxcols) {
    echo "<td> </td>";
    $i++;
}

//Close the table row and the table
echo "</tr>";
echo "</table>";

I haven't tested it yet but my wild guess is something like that. Just cycle through your dataset with the images and as long as you didn't make 5 <td>'s yet, add one. Once you reach 5, close the row and create a new row.

This script is supposed to give you something like the following. It obviously depends on how many images you have and I assumed that 5 (defined it in $maxcols) was the maximum number of images you want to display in a row.

<table>
    <tr>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
    </tr>
    <tr>
        <td><img src="image1.jpg" /></td>
        <td><img src="image1.jpg" /></td>
        <td> </td>
        <td> </td>
        <td> <td>
    </tr>
</table>
君勿笑 2024-12-18 21:33:04
$max_per_row = 5;
$item_count = 0;

echo "<table>";
echo "<tr>";
foreach ($images as $image)
{
    if ($item_count == $max_per_row)
    {
        echo "</tr><tr>";
        $item_count = 0;
    }
    echo "<td><img src='" . $image . "' /></td>";
    $item_count++;
}
echo "</tr>";
echo "</table>";
$max_per_row = 5;
$item_count = 0;

echo "<table>";
echo "<tr>";
foreach ($images as $image)
{
    if ($item_count == $max_per_row)
    {
        echo "</tr><tr>";
        $item_count = 0;
    }
    echo "<td><img src='" . $image . "' /></td>";
    $item_count++;
}
echo "</tr>";
echo "</table>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文