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 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重