Jump to content

Creating dynamic page numbers and related links in PHP?


Jagand

Recommended Posts

I am creating a webpage and one of my requirements is to fetch records from database. If the query results in say 30 records, I should display first 10 records in current page and create dynamic links numbered 2,3 at bottom of the page. Since the logic that drives each of these pages is identical and the only variable I see is $starting_rec (which gives initial limit of the query), I just want to create single PHP + MySQL page and use some logic to get value of $starting_rec depending on the link that user has clicked. What is the best way to determine which link user clicked?

 

if (false != ($result = mysql_query('SELECT .. FROM table')) {
  if (0 < ($row_count = mysql_num_rows($result))) {
    $page = ctype_digit($_GET['p']) ? intval($_GET['p']) : 1;    

    $items_per_page = 20;
    $number_of_pages = floor($row_count / $items_per_page);
    $offset = ($page - 1) * $items_per_page;
    
    for ($index = $offset;
         $index < $row_count && $index < $offset + $items_per_page;
         ++$index) {
       $field1 = mysql_result($result, $index, 'field1');
       $field2 = mysql_result($result, $index, 'field2');
    }
  }
  mysql_free_result($result);
}

 

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.