如何将2行结果回显到包含2列

时间:2016-05-31 05:17:06

标签: php html mysql

我正在使用以下代码来回显我的表格中的结果。但是,结果会在每次循环时生成新行。但我希望数据库中的第一行数据在第一列中回显,然后转到下一行用于下一行数据。简单地说,请参考这里的图片: 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>";
}

4 个答案:

答案 0 :(得分:0)

保持计数并继续这样:

    $count = 0;
    while ($row = $result->fetch_object())
    {
        if ($count%2 == 0) {
            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>";

        $remainder = $count%2;
        if ($remainder%2 == 1) {
             echo "</tr>";
        }
        $count++;

    }

答案 1 :(得分:0)

尝试我的锻炼,它的工作就像你期望的结果一样完美

    <?php

     $result =array('name','hi','hlo','as');

    if(!empty($result)) {

    echo "<table border='1'>";
    $i=0;

    foreach($result as $row)
    {

   //  while ($row = $result->fetch_object()) { //here use like this 

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

        echo '<td>'.$row.'</td>';

        //echo "<td><b>" . $row->name . "</b></td><td>Address: " . $row->address . "</td></tr><tr><td>Phone no: " . $row->phoneno . "</td><td>Fax No:" . $row->faxno . "</td></tr><tr><td>Email: " . $row->email . "</td><td>Website: " . $row->website .
       // "</td>";  //and your code here like this 


        if($i==1)
        {
            echo "</tr>";

            $i=-1;
        }



      $i++;


    }

    echo "</table>";

    }

    ?>

<强>输出:

    -----------
    |name | hi|
    -----------
    | hlo | as|
`   -----------

答案 2 :(得分:0)

感谢所有回复。在从回复中得到一些想法之后,我设法通过使用下面的代码来解决它。我通过添加计数器进行更改并在php编码之前启动标记。

<table align="center" border="1">
<tr>          

<?php
include("connect.php");

$result = mysqli_query($conn, "SELECT * FROM contact WHERE category='other'");
if ($result->num_rows > 0)
{
    $count = 0;
    while ($row = $result->fetch_object())
    {
         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($count % 2 == 0){
            //nothing here
         }

         else{
             echo "</tr>";
             echo "<tr>";
         }
         $count++;
   }
   echo "</table>";
}

答案 3 :(得分:0)

希望以下编码会有所帮助。

<?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>";