2016-05-31 71 views
1

我正在使用下面的代码来回显表格中的结果。但是,结果会在每次循环时产生新的行。但是我希望数据库的第一行数据能够在第一列中回显,然后移动到下一行数据的下一列。简单地说,指的是图片浏览: Desired Outcome如何将2行结果回显到2列的表格

我想要什么:

$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>"; 
} 
+0

试试我的答案,它给你什么预期的结果 – JYoThI

回答

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

    } 
+0

我试过了,但这给了我相同的结果,每列显示两次。 (结果1 |结果1) –

+0

现在输出只有1列 –

+0

你能复制我现在编辑的代码并试试吗? –

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

    } 

    ?> 

OUTPUT:

----------- 
    |name | hi| 
    ----------- 
    | hlo | as| 
` ----------- 
+0

您的方法需要字符串数据,但我显示数据库中的数据,所以它不起作用 –

+0

从逻辑上讲,它可以很好地执行代码。 – JYoThI

+0

更改回显“​​”。 $ row-> name。 “​​地址:”。 $ row->地址。 “​​电话号码:”。 $ row-> phoneno。 “​​传真号码:”。 $ row-> faxno。 “​​电子邮件:”。 $ row-> email。 “​​网站:”。 $ row->网站。 “”;回声'​​'。$行'。'; – JYoThI

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>"; 
} 
+0

如果$ result-> fetch_object()为空表示创建半表?哈哈 – JYoThI

+0

@安妮谭。它不会每次都完美地工作。正如jothi所说,如果返回空行或返回奇数行。通过我的代码,它会很方便。 –