SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pagination - Display Current Page

    I currently have the following code, that limits 8 records to be displayed on a page. If more than 8 records are contained inside the table, more pages will be dynamically created, whilst enabling a "Next Screen" link in order to view the remaining records.

    How do I output the current page number, along with the total pages to the screen.

    Example Output:

    Page 1 of 2
    Code:
    <link rel="stylesheet" type="text/css" href="Index.css" />
    
    <?php
    mysql_connect("localhost","peter","abc123");
    mysql_select_db("TS");
    
    $perpage = 2;
    
    $start = (isset($_GET['id'])) ? $_GET['id'] : 0;
    
    $TotalRec = mysql_num_rows(mysql_query("SELECT * FROM Buttons"));
    
    $select = "SELECT * FROM Buttons LIMIT $start, $perpage";
    $result = mysql_query($select) or die(mysql_error());
    
    # Dislay your rows here in the loop
    while($row = mysql_fetch_array($result))
    {
    
      echo $row['title'];
      echo $row['body'];
    }
    
    if($start == 0)
    {
      echo "Previous Screen";
    }
    else
    {
      echo "<a href=\"./Display.php?id=" . ($start - $perpage) . "\">Previous Screen</a>";
    }
    
    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    
    if($start == $TotalRec - 1)
    {
      echo "Next Screen";
    }
    else
    {
      echo "<a href=\"./Display.php?id=" . ($start + $perpage) . "\">Next Screen</a>";
    } 
    
    ?>

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Current page: ($_GET['id'] / $perpage) + 1
    Total pages: round($TotalRec / $perpage)

    But, whoa. Whoa. Why are you selecting every row from the table in order to page it? That's totally inefficient!

    PHP Code:
    $TotalRec mysql_result(mysql_query("SELECT COUNT(*) FROM Buttons"), 0); 

  3. #3
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Current page: ($_GET['id'] / $perpage) + 1
    Total pages: round($TotalRec / $perpage)

    But, whoa. Whoa. Why are you selecting every row from the table in order to page it? That's totally inefficient!

    PHP Code:
    $TotalRec mysql_result(mysql_query("SELECT COUNT(*) FROM Buttons"), 0); 
    Cheers for pointing that out.
    How else can it be done?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Check the code you quoted, it's not the same as yours

  5. #5
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Check the code you quoted, it's not the same as yours
    Oh year, Cheers

  6. #6
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do I add CSS to the second line, as currenlty it doesn't work. Why?
    The following text should be display in White:

    1 of 1

    Instead it displays in black, and therefore is not linking to the CSS file.

    Code:
    echo  '<div id="Pag">';
    echo    '<div id="PagCap">' . ($_GET['id'] / $perpage) + 1 . "of" . round($TotalRec / $perpage) . '</div>';
    echo  '</div>';

  7. #7
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pagination - Display Current Page

    How do I change the code to display Page 1 of 1 if the table only contains one record. As currently the following is displayed:

    Page 1 of 0

    Also, if the amount of records contained in the table is the same as the $perpage value, how come the "Next Screen" link is enabled, this should be disable due to the amount of records stored in the table equaling the $perpage value. Why, what needs to be changed?

    Code:
    <link rel="stylesheet" type="text/css" href="Index.css" />
    
    <?php
    mysql_connect("localhost","peter","abc123");
    mysql_select_db("TS");
    
    $perpage = 3;
    
    $start = (isset($_GET['id'])) ? $_GET['id'] : 0;
    
    $TotalRec = mysql_result(mysql_query("SELECT COUNT(*) FROM Buttons"), 0);
    
    $select = "SELECT * FROM Buttons LIMIT $start, $perpage";
    $result = mysql_query($select) or die(mysql_error());
    
    # Dislay your rows here in the loop
    while($row = mysql_fetch_array($result))
    {
      echo $row['title'];
      echo $row['body'];
    }
    
    echo    '<div id="PagCap">' . ($_GET['id'] / $perpage) + 1 . "of" . round($TotalRec / $perpage) . '</div>';
    
    if($start == 0)
    {
      echo "Previous Screen";
    }
    else
    {
      echo "<a href=\"./Display.php?id=" . ($start - $perpage) . "\">Previous Screen</a>";
    }
    
    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    
    if($start == $TotalRec - 1)
    {
      echo "Next Screen";
    }
    else
    {
      echo "<a href=\"./Display.php?id=" . ($start + $perpage) . "\">Next Screen</a>";
    } 
    
    ?>


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
  •