【问题标题】:PHP Break Table Row on While mysqli_fetch_arrayPHP 在 mysqli_fetch_array 上打破表行
【发布时间】:2021-01-22 13:16:37
【问题描述】:

我有下面的代码显示表 td 中的数据,目前我正在显示四个表 td,但是当我增加 8 或 12 之类的 no 时,它很乱。我需要每 4 td 开始一个新行。我怎样才能做到这一点?

       <tr style="background-color:white;"> 
       <?php     
        while ($row = mysqli_fetch_array($rs_result)) {    
              // Display each field of the records.    
        ?>     
           
          <td style="vertical-align:middle;padding-top:0px;margin-top:0px;width:32px"><img styel="height:32px;width: 32px;" src="<?php echo $row["gravator"]; ?>"/></td>
            <td style="vertical-align:top;margin-left:10px;text-align:left !important;">
                <!--<span><img src="<?php echo $row["gravator"]; ?>"/</span>-->
                
                <span style="">#Ad id:<?php echo $row["id"]; ?></span><br>
                <span style="margin-left:2px;border-top:1px solid blue;" class="node-text"><?php echo $row["fullname"]; ?> Has Added</span> <br>
                
                <span style="position:absolute;margin-top:-15px;margin-left:2px;width:160px;" class="node-text"><a target="_blank" href="<?php echo $row["url"]; ?>"><?php echo $row["title"]; ?></a></span> <br>
                
                <span style="position:absolute;margin-top:-26px;font-size:smaller;color:silver;margin-left:2px;" class="node-text">On <?php echo $row["datetime"]; ?></span>
                
            </td>
        
        <?php     
            };    
        ?> </tr> 

【问题讨论】:

标签: php line-breaks


【解决方案1】:

嗯...这不是最美丽的方法...但我想它应该适合你。我为 max_cols 添加了一个计数器,并每隔 4 个单元格添加新的 &lt;tr&gt;

   <tr style="background-color:white;"> 
   <?php     
    $max_cols = 4;
    while ($row = mysqli_fetch_array($rs_result)) {    
          // Display each field of the records.    
    ?>     
       
      <td style="vertical-align:middle;padding-top:0px;margin-top:0px;width:32px"><img styel="height:32px;width: 32px;" src="<?php echo $row["gravator"]; ?>"/></td>
        <td style="vertical-align:top;margin-left:10px;text-align:left !important;">
            <!--<span><img src="<?php echo $row["gravator"]; ?>"/</span>-->
            
            <span style="">#Ad id:<?php echo $row["id"]; ?></span><br>
            <span style="margin-left:2px;border-top:1px solid blue;" class="node-text"><?php echo $row["fullname"]; ?> Has Added</span> <br>
            
            <span style="position:absolute;margin-top:-15px;margin-left:2px;width:160px;" class="node-text"><a target="_blank" href="<?php echo $row["url"]; ?>"><?php echo $row["title"]; ?></a></span> <br>
            
            <span style="position:absolute;margin-top:-26px;font-size:smaller;color:silver;margin-left:2px;" class="node-text">On <?php echo $row["datetime"]; ?></span>
            
        </td>
    
    <?php     
        $max_cols--;
        if ($max_cols == 0) {
            echo '</tr>   <tr style="background-color:white">'; 
            $max_cols = 4;
        }
        };    
    ?> </tr> 

【讨论】:

    【解决方案2】:

    我推荐你echo你的tr/td,每四行echotr

    $count = 1;
    while ($row = mysqli_fetch_array($rs_result)) {  
          echo $count % 4 == 0 ? "<tr>": '';
          echo "<td>Here is a content of your td</td>"
          echo $count % 4 == 0 ? "</tr>": '';
      }
    

    【讨论】:

      【解决方案3】:

      基本上,您需要计算while() 循环中的每个&lt;td&gt;&lt;/td&gt;。当该计数达到您的限制时,即:4,您开始一个新行并将计数器重置为 1。您可以使用 $maxTDs(如下)设置每行 &lt;td&gt;s 的数量。

      这未经测试,但应该可以工作和/或让您真正接近:

      <tr style="background-color:white;"> 
      <?php
      $tdCnt = 0; // to start
      $maxTDs = 4; // each row has 4 <td>s
      
      while ($row = mysqli_fetch_array($rs_result)) {
          if( $tdCnt == $maxTDs ) {
      
              # close the row
              print "</tr>";
      
              # start a new row;
              ?>
              <tr style="background-color:white;">
              <?php
      
              # reset $tdCnt
              $tdCnt = 0;
          }
      
          // Display each field of the records.  
          ?>     
      
          <td style="vertical-align:middle;padding-top:0px;margin-top:0px;width:32px"><img styel="height:32px;width: 32px;" src="<?php echo $row["gravator"]; ?>"/></td>
          <?php $tdCnt++; ?>
      
          <td style="vertical-align:top;margin-left:10px;text-align:left !important;">
              <!--<span><img src="<?php echo $row["gravator"]; ?>"/</span>-->
      
              <span style="">#Ad id:<?php echo $row["id"]; ?></span><br>
              <span style="margin-left:2px;border-top:1px solid blue;" class="node-text"><?php echo $row["fullname"]; ?> Has Added</span> <br>
      
              <span style="position:absolute;margin-top:-15px;margin-left:2px;width:160px;" class="node-text"><a target="_blank" href="<?php echo $row["url"]; ?>"><?php echo $row["title"]; ?></a></span> <br>
      
              <span style="position:absolute;margin-top:-26px;font-size:smaller;color:silver;margin-left:2px;" class="node-text">On <?php echo $row["datetime"]; ?></span>
      
          </td>
          <?php $tdCnt++; ?>
      
          <?php
      };    
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-16
        • 1970-01-01
        相关资源
        最近更新 更多