Jump to content

Limit While PDO MySQL Results


Nickmadd

Recommended Posts

How can I limit the amount of time my while loop to only show the first 4 rows of my SQL table?

 

I am using this code to loop my whole table:

 

while($row = $results->fetch(PDO::FETCH_ASSOC))
                        {
                        echo '
                        <li>Mileage: '.number_format($row["Mileage"]).'</li>
                        <li>Engine size: '.$row["EngineSize"].'cc</li>
                        ';} ?>

I only want to loop through the first 4 rows of my SQL table, I then want to duplicate the same code but start at the 5th row until the 8th row of the table, how can I do this?

 

Thanks, Nick

 

Link to comment
https://forums.phpfreaks.com/topic/291084-limit-while-pdo-mysql-results/
Share on other sites

Or store the results in an array and use array_chunk() with a chunk size of 4

EG

$sql = "SELECT user_id, username 
        FROM user
        ORDER BY username";
$res = $db->query($sql);
$rows = $res->fetch_all();
$chunks = array_chunk($rows, 4);

foreach ($chunks as $chunk) {
    echo "<table border='1' cellpadding='3'>
            <tr><th>Id</th><th>Name</th></tr>\n";
    foreach ($chunk as $row) {
        list($id, $name) = $row;
        echo "<tr><td>$id</td><td>$name</td></tr>\n";
    }
    echo "</table><br><br>\n";
}

post-3105-0-17125700-1410814822_thumb.png

Create a counter before the loop, and increment it by one just before the end of the loop and check the counter within the loop to process differently? Hard to say with the information provided.

 

 

Hello sorry for being a bit vague, basically I am wanting to have to loops on my page just like this:

                        while($row = $results->fetch(PDO::FETCH_ASSOC))
                        {
                        echo '
                        <li>Mileage: '.number_format($row["Mileage"]).'</li>
                        <li>Engine size: '.$row["EngineSize"].'cc</li>
                        ';} ?>


                        while($row = $results->fetch(PDO::FETCH_ASSOC))
                        {
                        echo '
                        <li>Mileage: '.number_format($row["Mileage"]).'</li>
                        <li>Engine size: '.$row["EngineSize"].'cc</li>
                        ';} ?>

I want the first block to loop the first 4 rows in the table, I then want the second block to to show the next 4 rows after the first so it isn't displaying the same strings as the first block.

 

I know this might seem a little strange however I need to do this, I hate to be cheeky but could you show me an example of a counter? I have searched Goole but cant find a solution, I'm still really nooby when it comes to PHP.

 

Thanks

Barand has a better solution, but mine was something like:

$count = 0; //keep track of where we are in the array
$separate = 4; //number of entries before new ul
echo '<ul>';
while($row = $results->fetch(PDO::FETCH_ASSOC))
{
  //If we hit $separate by checking modulus operator, close ul and start a new one
  if ($x%$separate == 0)
  {
    echo '</ul><ul>';
  }

  //output li's
  echo '<li>Mileage: '.number_format($row["Mileage"]).'</li>';
  echo '<li>Engine size: '.$row["EngineSize"].'cc</li>';

  //increment counter
  $count++;
}
echo '</ul>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.