简体   繁体   中英

How to split SQL query results into chunks in PHP

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

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>

First! move that logic far away from the html, that will allow you to see the problem more clearly. Second! what about two classes a Class A that represents an n-length list of query results. Class A should be an iterator, so that a Class B can pull arbitrary length chunks of results and return appropriately sized result objects. maybe there's a third class whose instances represent the sized constrained result. So all in all three classes A representing a full list of results, B acting on a and returning instances of C which would represent your sized constrained result chunks.

This was originally a comment, but it was too long, so I had to make it an answer (sorry if it's too 'teach a man to fish' I just don't want to write php...)

Get all your results, read them into a multidimensional array, and then split them into groups with array_chunk().

$query = mysql_query("SELECT * FROM dv_photo_work",$link);
$results = array();
while($line = mysql_fetch_array($query, MYSQL_ASSOC)){
    $results[] = $line;
}
$lists = array_chunk($results,4);
foreach ($lists as $list) {
    echo '<ul>';
    foreach ($list as $result) {
        echo '<li><img alt="'.$result["img_title"].'" src="'.$result["img_slug"].'></li>'
    }
    echo '</ul>';
}

Here is an outline of the logic involved, using the modulus operator (%) to change the HTML at every fourth record:

$i = 0;

// within your current while-loop:
    if ($i % 4 == 0) {
        // $i is a multiple of 4 (including 0, the first record)
        if ($i > 0) {
            // finish the previous ul-tag
        }
        // start a new block of HTML (the ul)
    }
    // output the li
    $i += 1;

// after the loop, close the last ul

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM