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?

 

Link to comment
Share on other sites

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);
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.