Using php to display 1-10, 11-20 results from a mysql query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Using php to display 1-10, 11-20 results from a mysql query

Post by Citizen »

Basically, i need to put the 1-10 results from a query in one html column adn 11-20 and so on in another column

What code do I use for this?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I'd suggest building an array with all of your results then using a for loop to limit the results per section.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

Hmm... I know how to build an array and I know how to use a loop, but what do I do with the loop to get those results?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

do your initial loop over the results with while() to build your array().

then use a for() loop to loop over your specified number of results.

edit:
for your for loop you'd do something like this:

Code: Select all

for($i=0;$i<10;$i++)
     echo $myArray[$i][$value];
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

the trick is to search the web (or this forum) for 'pagination'.

pagination for all.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I'm not sure pagination is what the OP is looking for. It sounds like he wants to take X number of results and put them into a certain location in his markup. Then take another X number of the same result set and place them into a different location in his markup.

Code: Select all

<?php
$array_count = count($myarray);
echo '<table><tr><td>';
for ($i = 0; $i < $array_count; $i++) {
    echo "This is the array value for iteration $i..." . $myarray[$i]['value'] . "<br />";
    if ($i % 10) {
        if ($i != $array_count - 1) {
            echo '</td><td>';
        }
    }
}
echo '</td></tr></table>';
?>
I am sure there a ton of better ways to do this, but this might point you in a direction that could help.

PS The code above runs all of the results in the $myarray array through one long rowed table, with 10 results in each cell.
Post Reply