SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2012
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Numbering Items does not continue with pagination.

    Hey folks,

    I have a table that fetched items out of the database and is paginated. I wanted to include a number next to every item to make it more viewer friendly.
    Here is the code:
    Code:
    if(!isset( $_GET['p']) ){$_GET['p'] =0;} 
      $per_page=  2;
      $sql= "SELECT * FROM items" ;
      $rows= mysql_num_rows ( mysql_query ($sql)) ;
      $pages= ceil($rows / $per_page);    
    $sql2 = "SELECT * FROM items WHERE `active` = 1 LIMIT ".$_GET['p'].",". $per_page;
    $query= mysql_query ($sql2) ;
    
    $n =1
    while ( $row = mysql_fetch_assoc ( $query ) ){
    echo $row['item'], $row['item2'], $n++;
    }
    for ( $i=0; $i<$pages; $i++){
       echo ($i == $_GET['p'] / $per_page) ' <a href="index.php?p=' . ($i * $per_page) . '>'.($i +1). '</a>';
    	}
    The code works fine, but when I go to the next page the numbers restart from 1.
    I tried several things such as
    Code:
    if ($_GET['p'] == 0){$n = 1}else{$n =6}
    Which would make sense if 5 items are displayed perpage.

    The problem is that since pressing page 2 does not refresh the site but only the table i am guessing the if statement only gets read once.

    Any ideas how to easily solve this problem?

    Thanks you very much

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    If you know pagenumber p, and you have n items per page, then the count should start from

    ((p - 1) * n) + 1

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2012
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you guido,

    The formula didn't work exactly but it made me realize that I can just plug a function in instead of $n and that made it very easy.

    Thanks again


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •