安排从表中的DB检索的数据

时间:2015-03-15 14:50:03

标签: php

我使用以下代码查看表格内的图片(从DB获取其信息)我想在每行中查看3-pic,当图片数量不是三个并发症时会出现问题,如:4 ,5,7,8 ..等等,第一列不包含三张pic 1或2,我希望这种情况在最后一行不在第一行。 问题形状:

* *
* * *
* * *

我想要它:

* * *
* * *
* *

这里是代码:

<?php
    $mysqli = new mysqli("localhost","root","","flip2");
    $query = "SELECT * FROM images";
    $query2 = "SELECT * FROM threads";

    $result=$mysqli->query($query) or die('Error, query failed'.mysql_error());
    $result2=$mysqli->query($query2) or die('Error, query     failed'.mysql_error());


    $i = mysqli_num_rows($result);

    while($row = mysqli_fetch_array($result) ){
        if($i%3==0){
            echo "<tr>";
        }

        $z=$row['threads_id'];
        $t=$mysqli->query("SELECT Title FROM threads WHERE threads.id=$z order by threads.id DESC ") or die('Error, query failed'.mysql_error());
        $t1=mysqli_fetch_array($t);
        $title=$t1['Title'];


        echo "<td>"."<figure class='cap-bot' id='fig'>".'<img src="'.$row['path'].'" id="c_img">'.
            "<figcaption id='fig_cap'>"."$title"."<br>"."<span>"."&nbsp click on : &nbsp &nbsp".
            "</span>"."<a onClick='choice($row[threads_id])'  href='c_info.php?id=$row[threads_id]' id='l1'>"."more details"."</a>"."</figcaption>"."<td>"."</figure>".
            "<form action='c_info.php' method='post'><input type='hidden' id='$z' name='kk' value='$z'></form>";

        $i--;
        if($i%3==0){
            echo "<tr>";
        }

    }

?>

1 个答案:

答案 0 :(得分:0)

在循环结束时将<tr>替换为</tr>。并从开始计算$i

$i = 0;
while ($row = mysqli_fetch_array($result) ){
    if ($i%3==0){
        echo "<tr>";
    }

    // your code here

    if ($i%3==0){
        echo "</tr>"; // TR should be closed, not another one opened 
    }
    $i++;
}