Nickmadd Posted September 15, 2014 Share Posted September 15, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/291084-limit-while-pdo-mysql-results/ Share on other sites More sharing options...
CroNiX Posted September 15, 2014 Share Posted September 15, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291084-limit-while-pdo-mysql-results/#findComment-1491199 Share on other sites More sharing options...
Barand Posted September 15, 2014 Share Posted September 15, 2014 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/291084-limit-while-pdo-mysql-results/#findComment-1491203 Share on other sites More sharing options...
Nickmadd Posted September 15, 2014 Author Share Posted September 15, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/291084-limit-while-pdo-mysql-results/#findComment-1491206 Share on other sites More sharing options...
CroNiX Posted September 15, 2014 Share Posted September 15, 2014 (edited) 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>'; Edited September 15, 2014 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/291084-limit-while-pdo-mysql-results/#findComment-1491211 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.