使用PHP创建动态表

max*_*n15 5 php html-table dynamic dynamic-data dynamic-tables

我正在尝试用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>
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

此代码创建一行.我想不出怎么能得到多行...... !!

Jul*_*les 12

$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>&nbsp;</td>";
    $i++;
}

//Close the table row and the table
echo "</tr>";
echo "</table>";
Run Code Online (Sandbox Code Playgroud)

我还没有对它进行过测试,但我的猜测是这样的.只需使用图像循环访问数据集,只要您还没有使用5 <td>,请添加一个.到达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>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;<td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)