I am writing an article script and just want to see what is the best way to construct a while loop to show between 0 and 10 then offer links to different pages etc for 10 to 20. Currently its something like:

while($row = mysql_fetch_array($query)) {
echo "<tr><td>$row[id]</td><td>$row[title]</td></tr>";
}
    for ($i = 0; $i < 10; $i++) {
        $row = mysql_fetch_array($query) or break;
        // do the thing for the first 10 items here
    }
    for ($i = 10; $i < 20; $i++) {
        $row = mysql_fetch_array($query) or break;
        // do the thing for the next 10 items here
    }
    

    The for gives you the loops, each one getting 10 rows from mysql_fetch_array. If mysql_fetch_array runs out of rows, the "break" statement will end that loop.

    [Edit]
    I misread the question. Using "SELECT ... LIMIT ..." is what you want, as suggested by Planetsim.

      You need it in the SQL statement not a for loop

      $page = (!isset($_GET['page']))? '0' : $_GET['page'];
      
      $query = mysql_query("SELECT * FROM mytable LIMIT $page, 10");
      
      $numrows = mysql_num_rows(mysql_query("SELECT * FROM mytable"));
      
      while ($row = mysql_fetcharray($query)) {
        echo "<tr><td>$row['id']</td><td>$row['title']</td></tr>"; 
      }
      
      //pagination here
      
      
      

      Just create your pagination script. I couldnt find the one i use.

        Pagination script? you lost me.

        Yes I am new at this. 😃

        Can anyone explain the best way to do this? I want it to automatically generate it all as new articles are added, so that while there are under 10 links it will display them, then once they go over 10 it will display a link to the next 10 links. I also need them ordered by date.

          See also this forum's FAQ for a couple of links to other pagination scripts.

            Write a Reply...