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

报告相同问题?

悬赏问题

  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案