Can someone please help me with Pagination?

Discussion in 'Programming' started by Kurt Whittingham, Apr 10, 2012.

  1. #1
    I really need help with making a Pagination?

    Thanks
    Kurt
     
    Kurt Whittingham, Apr 10, 2012 IP
  2. aisha khatoon

    aisha khatoon Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    some tips for you.
    1. INCREASE ITS SIZE, small pagination not good.
    .
    2. MAKE THE CLICK-ABLE AREA LARGER.
    3. MAKE IT OBVIOUS.
     
    aisha khatoon, Apr 10, 2012 IP
  3. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #3
    All obvious...and nothing to do with the original posters question.

    Kurt what language are you using? I have a PHP class I created for Pagination.
     
    NetStar, Apr 10, 2012 IP
  4. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #4
    NetStar yes i am using php
     
    Kurt Whittingham, Apr 10, 2012 IP
  5. aisha khatoon

    aisha khatoon Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    [h=3]1. Obtain the required page number[/h][h=3]2. Identify how many database rows are available[/h][h=3]3. Calculate number of $lastpage[/h][h=3]4. Ensure that $pageno is within range[/h][h=3]5. Construct LIMIT clause[/h][h=3]6. Issue the database query[/h]
    [h=3]7. Construct pagination hyperlinks[/h]
     
    aisha khatoon, Apr 11, 2012 IP
  6. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #6
    How would i do this?
     
    Kurt Whittingham, Apr 11, 2012 IP
  7. Tortemon

    Tortemon Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    What have you already made? You can show us your code, and then we will help you.

    Except that, you can use ready solutions, google it.
     
    Tortemon, Apr 11, 2012 IP
  8. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #8
    <?php
    
    /**************************************************************************************************
    * =================================================================================================
    * PHP mySQL Paging Class 1.0
    * class_resultpaging.php
    * - DESCRIPTION
    *     A PHP function that enables you to create page links for your database results.
    *     Function will return formatted page links and mySQL LIMIT code.
    * =================================================================================================
    * Author:  Jay Juliano - jayjuliano@gmail.com
    * Created: 12/23/2008 (When I should of been wrapping presents!)
    * =================================================================================================
    * Copyright 2008-2009 - NetStar Interactive LLC
    * =================================================================================================
    * You are free to use, distribute, and modify this software under the terms of the
    * GNU General Public License.  See http://www.gnu.org/copyleft/gpl.html for more details.
    **************************************************************************************************/
    
    class ResultPaging {
          function getPageLinks($sql, $params = "")
          {
                   $__ResultPaging_DisplayPages   = 5;
                   $__ResultPaging_ResultsPerPage = 10;
                   $__ResultPaging_PageParam      = "page";
    
                   if (!isset($sql)) die("Missing SQL for getPageLinks()\n");
    
                   $params = isset($params) ? $params : "";
                   if ($params && !preg_match("/&$/", $params)) $params .= "&";
                   $params = preg_replace("/^&/", "", $params);
                   $params = preg_replace("/^\?/", "", $params);
    
                   $page = "";
                   if (!isset($_GET[$__ResultPaging_PageParam]) || !is_numeric($_GET[$__ResultPaging_PageParam]) || $_GET[$__ResultPaging_PageParam] == 0) {
                      $page = 1;
                   } else {
                       $page = $_GET[$__ResultPaging_PageParam];
                   }
                   
                   $total_results  = mysql_num_rows(mysql_query($sql));
                   
                   $total_pages = 0;
                   if ($total_results) $total_pages = ceil($total_results / $__ResultPaging_ResultsPerPage);
                   
                   if ($page > $total_pages) $page = $total_pages;
                   
                   $starting_record = ($page * $__ResultPaging_ResultsPerPage) - $__ResultPaging_ResultsPerPage;
                   
                   if ($starting_record < 0) $starting_record = 0;
                   
                   $start_page = "";
                   $end_page   = "";
                   if ($total_pages == 0) {
                      $start_page = 0;
                      $end_page   = 0;
                   } else if ($total_pages <= $__ResultPaging_DisplayPages) {
                       $start_page = 1;
                       $end_page   = $total_pages;
                   } else {
                       $page_set   = ceil($page / $__ResultPaging_DisplayPages) - 1;
                       $start_page = ($page_set * $__ResultPaging_DisplayPages) + 1;
                           
                       if ($total_pages < $start_page + $__ResultPaging_DisplayPages - 1) {
                          $end_page = $total_pages;
                       } else {
                           $end_page = ($start_page + $__ResultPaging_DisplayPages) - 1;
                       }
                   }
                   
                   $previous_pageset_end   = $start_page - 1;
                   $next_pageset_start     = $end_page   + 1;
                   $previous_pageset_start = $previous_pageset_end - $__ResultPaging_DisplayPages + 1;
                   $next_pageset_end       = $next_pageset_start   + $__ResultPaging_DisplayPages - 1;
                   
                   $previous_page    = $page - 1;
                   $next_page        = $page + 1;
                   
                   $first_record  = $starting_record + 1;
                   $last_record   = $first_record    + $__ResultPaging_ResultsPerPage - 1;
                   
                   if ($total_results < $last_record) $last_record = $total_results;
                   
                   /**************************************************************************************************
                     * Format Link Pages
                     **************************************************************************************************/
                   $pagesHTML = "<div id=\"pagesHTML\" style=\"width: 100%; border-bottom: 1px solid #000000; font-family: verdana, arial; font-size: 8pt;\">";
                   
                   if ($first_record == 1 && $total_results < 1) $first_record = 0;
                   
                   $pagesHTML .= "<div id=\"pagesHTMLListing\" style=\"width: 33%; float: left;\">Listing $first_record-$last_record of $total_results</div>";
                   
                   $pagesHTML .= "<div id=\"pagesHTMLPages\" style=\"width: 33%; float: left;\">";
                   
                   if ($total_results > 0) {
                      if ($start_page > $__ResultPaging_DisplayPages) $pagesHTML .= "<a href=\"" . $_SERVER['PHP_SELF'] . "?" . $params . "page=1\" title=\"Page 1\" style=\"font-weight: bold;\">1</a>&nbsp;&nbsp;&nbsp;<a href=\"" . $_SERVER['PHP_SELF'] . "?" . $params . "page=$previous_pageset_end\" title=\"&lsaquo;&lsaquo; Pages $previous_pageset_start-$previous_pageset_end\" style=\"font-weight: bold;\">&lsaquo;&lsaquo;</a>&nbsp;&nbsp;&nbsp;";
                   
                      for ($i = $start_page; $i <= $end_page; $i++)
                      {
                          if ($i == $page) {
                             $pagesHTML .= "<span style=\"font-weight: bold;\">$i</span>&nbsp;&nbsp;&nbsp;";
                          } else {
                              $pagesHTML .= "<a href=\"" . $_SERVER['PHP_SELF'] . "?" . $params . "page=$i\" title=\"Page $i\" style=\"font-weight: bold;\">$i</a>&nbsp;&nbsp;&nbsp;";
                          }
                      }
    
                      if ($end_page != $total_pages) $pagesHTML .= "<a href=\"" . $_SERVER['PHP_SELF'] . "?" . $params . "page=$next_pageset_start\" title=\"Pages $next_pageset_start-$next_pageset_end &rsaquo;&rsaquo;\" style=\"font-weight: bold;\">&rsaquo;&rsaquo;</a>&nbsp;&nbsp;of&nbsp;<a href=\"" . $_SERVER['PHP_SELF'] . "?" . $params . "page=$total_pages\" title=\"Page $total_pages\" style=\"font-weight: bold;\">$total_pages</a>";
                   }
                   
                   $pagesHTML .= "</div>";
                   
                   $pagesHTML .= "<div id=\"pagesHTMLPageSwitches\" style=\"width: 33%; float: left; text-align: right;\">";
                   if (($page > 1 && $page <= $total_pages) || ($page < $total_pages)) {
                      $pagesHTML .= "&nbsp;&nbsp;&nbsp;";
                      if ($page > 1 && $page <= $total_pages) $pagesHTML .= "<a href=\"" . $_SERVER['PHP_SELF'] . "?" . $params . "page=$previous_page\" title=\"&lsaquo; Previous Page $previous_page\" style=\"font-weight: bold;\">&lsaquo; Previous</a>";
                      if (($page > 1 && $page <= $total_pages) && ($page < $total_pages)) $pagesHTML .= "&nbsp;|&nbsp;";
                      if ($page < $total_pages) $pagesHTML .= "<a href=\"" . $_SERVER['PHP_SELF'] . "?" . $params . "page=$next_page\" title=\"Next Page $next_page &rsaquo;\" style=\"font-weight: bold;\">Next &rsaquo;</a>";
                   }
                   $pagesHTML .= "</div>";
    
                   $pagesHTML .= "<div style=\"clear: left;\"></div>";
                   
                   $pagesHTML .= "</div>";
    
                   $limit_sql = "LIMIT $starting_record, $__ResultPaging_ResultsPerPage";
                   
                   return array($pagesHTML, $limit_sql);
          }
    }
    
    ?>
    
    PHP:
    Usage:

    
    #...
    include "class_resultpaging.php";
    #...
    $q = $_GET['q'];
    $sql = "SELECT name, address, address2, city, state, state_prefix, name_url, zip, phone, type, gender, orientation
                               FROM Whatever
                               WHERE MATCH (name, city) AGAINST ('" . safe($q) . "')";
    
    $paging = new ResultPaging();
    $pagelinkArray = $paging->getPageLinks("$sql;", "q=$q");
    $pageLinks = $pagelinkArray[0];
    $pageLimit = $pagelinkArray[1];
    
    $result = mysql_query("$sql $pageLimit;");
    #...
    ...
    PHP:
    $sql holds the initial query which is passed to the class... The class will then run the query and figure out how many pages and return the SQL with the SQL Limit info... Then you run the new query like shown above...

    $pageLinks will now hold the HTML for the paging with the current page etc.

    The query string "page" is used top identify current page and requested page...

    $q is additional query string appended to each page link...

    Play around with it and you will figure it out...
     
    NetStar, Apr 11, 2012 IP
  9. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #9
    wow im really confused sorry, im not that great with php yet..
    the page that is put on is this http://www.motorbikecentral.com/supercross race reviews.php

    this is the code that gets the data from my database.
    <?php require('connect.php');
        
    
        $result = mysql_query("SELECT * FROM supercross_race_reviews ORDER BY id DESC") 
        or die(mysql_error());  
        
        echo "<table width='90%' cellspacing='10'>";
        // keeps getting the next row until there are no more to get
        while($row = mysql_fetch_array( $result )) {
        // Print out the contents of each row into a table
        echo "<tr><td>"; 
        // arrow
        echo ("<img src='arrow.gif'>"); echo "&nbsp;&nbsp;";
        // article name
        echo '<a style="font-size: 25px" href="'. $row['url'] .'">'. $row['article_name'] .'</a>';
        echo "</td></tr><tr><td>"; 
        // author + date
        echo $row['author']; echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; echo $row['date'];
        echo "</td></tr><tr><td>";
        // Description
        echo "Brief Decription : "; echo "&nbsp;&nbsp;"; echo $row['description'];
        echo "<hr /></td></tr>";
    }
    
        ?>                
    
    PHP:
     
    Kurt Whittingham, Apr 11, 2012 IP
  10. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #10
    The class I posted simply generates the appropriate html for the page links and handles the SQL query. It will return the query for you to run that will return the results.
     
    NetStar, Apr 12, 2012 IP