[ACCEPTED]-How to limit items from while loop-while-loop

Accepted answer
Score: 15

In SQL:

$select = "SELECT * FROM nk_showcase LIMIT 0,10";

or in PHP:

$counter = 0;
$max = 10;

 while (($user = $db->fetch($query)) and ($counter < $max))
  {
   ... // HTML code here....

   $counter++;
  }

As to the rotating, see 1 @Fayden's answer.

Score: 4

Rotate as in random, or as the next 10 elements 6 ?

Most RDBMS allow you to order rows by random 5 :

-- MySQL
SELECT * FROM nk_showcase ORDER BY RAND() LIMIT 10
-- PostgreSQL
SELECT * FROM nk_showcase ORDER BY RANDOM() LIMIT 10

Which would select 10 random rows every 4 time you refresh the page

If you want to 3 show the next 10 elements, you would have 2 to paginate the page (and use the LIMIT 1 X OFFSET Y syntax)

Score: 0

You have to change your query $select, try 8 using LIMIT to 10 if you just need the 10 first 7 items or try also with OFFSET if you need to paginate 6 the results.

$select.=" OFFSET $start LIMIT $range;";

Then you need to control the 5 $start and $range variables like:

$size_page=10;
 if (!$page) {
             $start = 0;
             $page=1;
        }
        else {
            $start = ($page - 1) * $size_page;
        } 

You can notice that 4 $range should be the same value of $size_page 3 and just you need to calculate the $start 2 value for each iteration taking into account 1 the #pages

More Related questions