SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with php/mysql and pagination please.

    I cannot get the followin code to work. Keep getting an error Notice:undefined variable: start in ps_pagination.php on line 161. Below is my code for both pages. I have googled this but cannot figure out where the problem is, other people have used simailar code. Any help would be greatly appreciated.
    ----------ps_pagination.php code-------------------------------------------------------------------------------------------------
    PHP Code:
    <?php

    class PS_Pagination {
        var 
    $php_self;
        var 
    $rows_per_page//Number of records to display per page
        
    var $total_rows//Total number of rows returned by the query
        
    var $links_per_page//Number of links to display per page
        
    var $sql;
        var 
    $debug false;
        var 
    $conn;
        var 
    $page;
        var 
    $max_pages;
        var 
    $offset;
        
        
    /**
         * Constructor
         *
         * @param resource $connection Mysql connection link
         * @param string $sql SQL query to paginate. Example : SELECT * FROM users
         * @param integer $rows_per_page Number of records to display per page. Defaults to 10
         * @param integer $links_per_page Number of links to display per page. Defaults to 5
         */
         
        
    function PS_Pagination($connection$sql$rows_per_page 10$links_per_page 5) {
            
    $this->conn $connection;
            
    $this->sql $sql;
            
    $this->rows_per_page $rows_per_page;
            
    $this->links_per_page $links_per_page;
            
    $this->php_self htmlspecialchars($_SERVER['PHP_SELF']);
            if(isset(
    $_GET['page'])) {
                
    $this->page intval($_GET['page']);
            }
        }
        
        
    /**
         * Executes the SQL query and initializes internal variables
         *
         * @access public
         * @return resource
         */
        
    function paginate() {
            if(!
    $this->conn) {
                if(
    $this->debug) echo "MySQL connection missing<br />";
                return 
    false;
            }
            
            
    $all_rs = @mysql_query($this->sql);
            if(!
    $all_rs) {
                if(
    $this->debug) echo "SQL query failed. Check your query.<br />";
                return 
    false;
            }
            
    $this->total_rows mysql_num_rows($all_rs);
            @
    mysql_close($all_rs);
            
            
    $this->max_pages ceil($this->total_rows/$this->rows_per_page);
            
    //Check the page value just in case someone is trying to input an aribitrary value
            
    if($this->page $this->max_pages || $this->page <= 0) {
                
    $this->page 1;
            }
            
            
    //Calculate Offset
            
    $this->offset $this->rows_per_page * ($this->page-1);
            
            
    //Fetch the required result set
            
    $res = @mysql_query($this->sql." LIMIT {$this->offset}{$this->rows_per_page}");
            if(!
    $res) {
                if(
    $this->debug) echo "Pagination query failed. Check your query.<br />";
                return 
    false;
            }
            return 
    $res;
        }
        
        
    /**
         * Display the link to the first page
         *
         * @access public
         * @param string $tag Text string to be displayed as the link. Defaults to 'First'
         * @return string
         */
        
    function renderFirst($tag='First') {
            if(
    $this->page == 1) {
                return 
    $tag;
            }
            else {
                return 
    '<a href="'.$this->php_self.'?page=1">'.$tag.'</a>';
            }
        }
        
        
    /**
         * Display the link to the last page
         *
         * @access public
         * @param string $tag Text string to be displayed as the link. Defaults to 'Last'
         * @return string
         */
        
    function renderLast($tag='Last') {
            if(
    $this->page == $this->max_pages) {
                return 
    $tag;
            }
            else {
                return 
    '<a href="'.$this->php_self.'?page='.$this->max_pages.'">'.$tag.'</a>';
            }
        }
        
        
    /**
         * Display the next link
         *
         * @access public
         * @param string $tag Text string to be displayed as the link. Defaults to '>>'
         * @return string
         */
        
    function renderNext($tag=' &gt;&gt;') {
            if(
    $this->page $this->max_pages) {
                return 
    '<a href="'.$this->php_self.'?page='.($this->page+1).'">'.$tag.'</a>';
            }
            else {
                return 
    $tag;
            }
        }
        
        
    /**
         * Display the previous link
         *
         * @access public
         * @param string $tag Text string to be displayed as the link. Defaults to '<<'
         * @return string
         */
        
    function renderPrev($tag='&lt;&lt;') {
            if(
    $this->page 1) {
                return 
    '<a href="'.$this->php_self.'?page='.($this->page-1).'">'.$tag.'</a>';
            }
            else {
                return 
    $tag;
            }
        }
        
        
    /**
         * Display the page links
         *
         * @access public
         * @return string
         */
        
    function renderNav() {
            for(
    $i=1;$i<=$this->max_pages;$i+=$this->links_per_page) {
                if(
    $this->page >= $i) {
                    
    $start $i;
                }
            }
            
            if(
    $this->max_pages $this->links_per_page) {
                
    $end $start+$this->links_per_page;
                if(
    $end $this->max_pages$end $this->max_pages+1;
            }
            else {
                
    $end $this->max_pages;
            }
                
            
    $links '';
            
            for( 
    $i=$start $i<$end $i++) {
                if(
    $i == $this->page) {
                    
    $links .= $i ";
                }
                else {
                    
    $links .= ' <a href="'.$this->php_self.'index.php?menukey=11&page='.$i.'">'.$i.'</a> ';
                }
            }
            
            return 
    $links;
        }
        
        
    /**
         * Display full pagination navigation
         *
         * @access public
         * @return string
         */
        
    function renderFullNav() {
            return 
    $this->renderFirst().'&nbsp;'.$this->renderPrev().'&nbsp;'.$this->renderNav().'&nbsp;'.$this->renderNext().'&nbsp;'.$this->renderLast();    
        }
        
        
    /**
         * Set debug mode
         *
         * @access public
         * @param bool $debug Set to TRUE to enable debug messages
         * @return void
         */
        
    function setDebug($debug) {
            
    $this->debug $debug;
        }
    }
    ?>
    ---------------------paginate_customers.php code-------------------------------------------------------------------------------
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <title>IT-5334: PHP and MySQL</title>
        <!-- <link href="default.css" rel="stylesheet" type="text/css" /> -->
    </head>
    <body>

    <?php
        
    //Include the PS_Pagination class
        
    include('ps_pagination.php');
        require_once (
    'mysqli_connect.php');
        
        
    //Connect to mysql db
        
    $conn mysqli_connect('localhost''name''password');
        
    $sql 'SELECT * FROM customers ORDER by CustomerID';
        
    mysqli_select_db($conn'database name');
          
    $r = @mysqli_query($conn$sql); 
        if(!
    $r) { 
           die(
    mysqli_error($conn)); 
        } 
        
        
        
    //Create a PS_Pagination object
        
    $countperpage 20;
        
    $pager = new PS_Pagination($conn,$sql,$countperpage,6);
        
        
    //The paginate() function returns a mysql result set 
        
    $rs $pager->paginate();
        echo 
    '<table align="center" cellspacing="0" cellpadding="0" border=1>
            <tr>
                <td align="left" colspan="13"><h1>Customers:
    Last edited by SpacePhoenix; Oct 17, 2012 at 12:14. Reason: placed php tags around php code

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    That class expects a mysql connection not a mysqli connection.


Tags for this Thread

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
  •