drrc61668568 2013-06-27 01:49
浏览 61
已采纳

如何在PHP中将SQL查询结果拆分为块

I am trying to break query results into chunks using PHP but can't seem to figure out how to formulate either a query or a loop to break the results into batches of four and surround those four rows with specific HTML elements.

The content is specifically a photography portfolio, where I am just attempting to display photos row by row, but the way I need to style it calls for each row to basically be a separate set of information.

This is the code I'm working with thus far:

<ul class="no-bullet">
<?php

require("connect.php");
$query = "SELECT * FROM dv_photo_work LIMIT 4";

$result = mysql_query($query, $link);
while ($row = mysql_fetch_array($result)) {
  print('
     <li><img alt="'.$row["img_title"].'" src="'.$row["img_slug"].'></li>');
}


?>
</ul>

I only want four images to be in that list though. And then another four in another list. Can you help me reformulate the loop to make it return four records and then begin a new query of the following four records in another list? There will be approximately 100 records in the database that I will be retrieving this from, so it would have to be able to continue until the entire table is listed.

I imagine this is similar to a pagination process, yet that is another technique that I am unfamiliar with. I'm assuming it's a simple adjustment to the query and mark-up, however I could understand it being something that requires a bit more code creativity than a simple fix.

Many thanks,

Joshie

  • 写回答

4条回答 默认 最新

  • dougui5419 2013-06-27 02:08
    关注

    Try this

    <ul class="no-bullet">
    <?php
    require("connect.php");
    $query = "SELECT * FROM dv_photo_work";
    $result = mysql_query($query, $link);
    $i =1;
    while($row = mysql_fetch_array($result)) {
       print('<li><img alt="'.$row["img_title"].'" src="'.$row["img_slug"].'></li>');
       if ($i%4 == 0) echo "</ul><ul class='no-bullet'>";
       $i++;
    }
    ?>
    </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错