我正在使用以下代码从表中回显结果。但是,结果在每次循环时都会产生新的一行。但是我希望数据库中的第一行数据在第一列中回显,然后移至下一列以获取下一行数据。简单地说,请参考此处的图片:
Desired Outcome

我想要的是:

$result = mysqli_query($conn, "SELECT * FROM contact WHERE category='other'");
if ($result->num_rows > 0)
{
    while ($row = $result->fetch_object())
    {
        echo "<tr>";
        echo "<td width=500><b>". $row->name . "</b><br/>Address: ". $row->address . "<br/>Phone no: ". $row->phoneno . "<br/>Fax No:". $row->faxno ."<br/>Email: ". $row->email . "<br/>Website: ". $row->website ."</td>";
        //echo "</tr>";
        //echo "<tr>";
        //fetch next object here
        echo "<td  width=500><b>". $row->name . "</b><br/>Address: ". $row->address . "<br/>Phone no: ". $row->phoneno . "<br/>Fax No:". $row->faxno ."<br/>Email: ". $row->email . "<br/>Website: ". $row->website ."</td>";
        echo "</tr>";
    }
    echo "</table>";
}

最佳答案

希望下面的编码会有所帮助。

<?php
$i = 0;
echo "<table border=1>";
while($row = mysqli_fetch_object($results)) {
    $i++;
    if($i % 2 == 1) {
        echo "<tr>";
    }
    echo "<td width=500><b>". $row->name . "</b><br/>Address: ". $row->address . "<br/>Phone no: ". $row->phoneno . "<br/>Fax No:". $row->faxno ."<br/>Email: ". $row->email . "<br/>Website: ". $row->website ."</td>";
    if($i % 2 == 0) {
        echo "</tr>";
    }
}
if($i % 2 == 1) { // Used to prevent alignment issue while odd number of rows return
    echo "<td></td></tr>";
}
echo "</table>";

关于php - 如何将2行结果回显到2列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37537326/

10-12 12:35
查看更多