简体   繁体   English

如何在php中的数据库中每行只显示几个选项

[英]how to display only few options per line from database in php

I am fetching data from the database and displaying it in the table.我正在从数据库中获取数据并将其显示在表中。 I have applied a while loop before <tr> tag so it is showing each value in new line.我在<tr>标记之前应用了一个 while 循环,因此它在新行中显示每个值。

Now what i want is that it should show five values from database in each <tr> tag.现在我想要的是它应该在每个<tr>标签中显示来自数据库的五个值。 How can i do this ?我怎样才能做到这一点 ?

Here is my Code :这是我的代码:

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

Try this:尝试这个:

You need to start on first record and close on last record of every row.您需要从第一条记录开始并在每行的最后一条记录上关闭。

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

to show 5 records per line use显示每行使用的 5 条记录

if ($counter % 5 == 0) inside your while loop; if ($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>

Try this-尝试这个-

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

you can use counter variable,like.您可以使用计数器变量,例如。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM