dongsuyou6938 2017-04-15 20:14
浏览 19
已采纳

PHP在行数除以4后得到<div>

I need to divided all data into 4 columns. i have used this method but its not coming column property

   $stmt1 = $db->prepare($sql1);
   $stmt5 = $db->prepare($sql1);
   $stmt5->execute();

   $rowcount = $stmt5->rowCount();

   $pages = ceil($rowcount / 4);
   $tempcount = 1;

if ($stmt1->execute(array())) {
   while ($row = $stmt1->fetch()) {
   ?>

       <?php if ($tempcount == $pages) { ?>

         <div class="column">
         <div class="ui bulleted list">

      <?php } ?>

          <a href="" class="item">  </a>

     <?php
     if ($tempcount == $pages) {
      $pages = $pages + $pages; ?>

          </div>
          </div>

     <?php }
     $tempcount++;
      }
       } ?>

how i can get this columns after getting count of data i have and add those divs in between.

for an Example 20 data rows 20/4 = 5 after each 5 data, i need start and end to be added

    <div class="column">
    <div class="ui bulleted list">

      <a href="" class="item">  </a>
      <a href="" class="item">  </a>
      <a href="" class="item">  </a>
      <a href="" class="item">  </a>
      <a href="" class="item">  </a>

   </div>
   </div>
   <div class="column">
    <div class="ui bulleted list">

      <a href="" class="item">  </a>
      <a href="" class="item">  </a>
      <a href="" class="item">  </a>
      <a href="" class="item">  </a>
      <a href="" class="item">  </a>

   </div>
   </div> 

// And so on

result will be like this

thank you very much

  • 写回答

3条回答 默认 最新

  • dq05304 2017-04-15 20:29
    关注

    Here's a proper code with comments

    $tempcount = 0;
    $pages = ceil($rowcount / 4);
    while ($row = $stmt1->fetch()) {
    
        // if result of % (Modulo) is zero - you need to start new column
        if ($tempcount % $pages == 0) {?>
        <div class="column">
             <div class="ui bulleted list">
    <?php
        }?>
         <a href="" class="item">ITEM</a>
    <?php
    
        // if result of % (Modulo) is (pages - 1) - you need to close previous column
        if ($tempcount % $pages == ($pages - 1)) {?>
            </div>
        </div>
    <?php
        }
        $tempcount++;
    }
    
    // check if you have to close previous column 
    // because it was not closed in a while loop
    
    
    if (0 < $rowcount) {
        if ($tempcount % $pages != 0) {?>
                </div>
            </div>
        <?php
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下