2014-03-27 23 views
-1

我從數據庫中提取數據並在表中顯示它。我已在<tr>標記之前應用了一個while循環,因此它將以新行顯示每個值。如何從數據庫中的每行只顯示幾個選項php

現在我想要的是,它應該顯示每個<tr>標記中的數據庫中的五個值。 我該怎麼做?

這裏是我的代碼:

 <body> 
     <table border="2px;"> 

     <tr> 
     <th>name</th> 
     </tr> 

     <?php 
     $sql="select * from tb_transport"; 
      $query=mysql_query($sql); 
     while($row=mysql_fetch_array($query)) 
      { 
     ?> 

      <tr> 
      <td><?php echo $row["name"]; ?></td> 
     </tr> 
       <?php 

      } ?> 
       </table> 
     </body> 
+0

什麼五個選項? –

+0

哦對不起...我編輯了我的帖子 – user3138522

+2

我沒有得到這個_in標籤在每個tag_? –

回答

1

試試這個:

你需要開始對第一條記錄,收盤每一行的最後一個記錄。

<body> 
      <table border="2px;"> 

      <tr> 
      <th>name</th> 
      </tr> 

      <?php 
      $sql="select * from tb_transport"; 
       $query=mysql_query($sql); 

      $counter = 0; 

      while($row=mysql_fetch_array($query)) 
       { 
       $counter++; 
       if($counter % 5 == 1) 
       { 
         ?><tr><?php 
       } 
      ?> 


       <td><?php echo $row["name"]; ?></td> 
       <?php  
if($counter % 5 == 0) 
       { 
         ?></tr><?php 
       } 



       } ?> 
        </table> 
      </body> 
+0

我們的回答很有幫助... thanx很多 – user3138522

+1

我們的回答是?或我的回答? Pl upvote它我的朋友.... –

+0

對不起你的回答...並且我有upvoted它 – user3138522

0

顯示5條記錄,每行使用

如果您($counter % 5 == 0) while循環中;

 <tr> 
    <th>name</th> 
    </tr> 

    <?php 
    $sql="select * from tb_transport"; 
     $query=mysql_query($sql); 
    $counter = 1; // set counter to 1 
      while($row=mysql_fetch_array($query)) 
     { 
    ?> 

     <tr> 
     <td><?php 
        if ($counter % 5 == 0) 
        echo $row["name"]; 
$counter++; 
        ?></td> 
    </tr> 
      <?php 

     } ?> 
      </table> 
    </body> 
+0

我沒有得到它只有while循環 – user3138522

+0

@ user3138522,我已經更新了代碼,請檢查它 – SULTAN

+0

它沒有做任何事情? – user3138522

0

嘗試這個 -

 <body> 
      <table border="2px;"> 

      <tr> 
      <th>name</th> 
      </tr> 

      <?php 
      $sql="select * from tb_transport"; 
       $query=mysql_query($sql); 
    echo "<tr>"; 
$number=1; 
      while($row=mysql_fetch_array($query)) 
       { 
      ?> 


       <td><?php echo $row["name"]; ?></td> 
    <?php 

       if ($number % 5 == 0) 
    {  
    echo "</tr>"."<tr>"; 
    } 

        <?php 
    $number++; 
       } ?> 
    </tr> 
        </table> 
      </body> 
+0

我沒有downvote你的答案.... – user3138522

+0

永遠不會想到!至少,你得到了你的答案,這很重要。 – sunny

+0

我打算對您的答案進行投票,因爲您的答案根據帖子的初始和第一次修改是正確的。 – SULTAN

0

您可以使用計數器變量,如。

<body> 
    <table border="2px;"> 
    <tr> 
     <th>name</th> 
    </tr> 
    <?php 
     $count=5; 
     $sql="select * from tb_transport"; 
     $query=mysql_query($sql); 
     while($row=mysql_fetch_array($query)) 
     { 
    ?> 

     if($count == 5){ echo "<tr>"} 

     <td><?php echo $row["name"]; 
     $counter--; 
      ?></td> 

     if($count == 1) { echo "</tr>";} 
    <?php 
     } ?> 
    </table> 
</body> 
+0

downvote的任何理由? –

+0

和我一樣,陽光燦爛,我們不知道爲什麼!在編輯問題之前,我們的答案滿足了請求者的要求 - 對照提供的信息。但是他們只是低估了答案,甚至沒有檢查什麼是最初的帖子!我打算提高你的答案。 – SULTAN

+0

好的謝謝,我的意圖是不復制你的答案,但是當我寫回答你已經發布它。 –

相關問題