pagination question

Discussion in 'PHP' started by smetten, Apr 29, 2008.

  1. #1
    hi,

    i have a question about pagination. I´ve found this pagination script, but how do i use it on my own code? i don´t know how to integrate it.

    hopefully someone can help

    pagination script:
    <? 
    
    define("SITEPATH", "http://www.my-site.com"); 
    
    function pagination($querystring,$page_number,$num_rows_to_return,$extra_url_info) { 
        global $mysql_queries_debugging; 
        global $dev_mode; 
        $base_url = SITEPATH.$extra_url_info; 
        if ($page_number == null){ 
            $page_number = 0; 
        } 
        $p_dash_position = strpos($base_url, '/p'); 
        if ($p_dash_position === false) { 
            // do nowt 
        } else { 
            $p_position = strrpos($base_url, "/p"); 
            $base_url_length = strlen($base_url); 
            $base_url = substr($p_position, $p_position, $base_url_length); 
        } 
        // If SQL_CALC_FOUND_ROWS inst in the string - add it! 
        if(stristr($querystring, 'SQL_CALC_FOUND_ROWS') === FALSE){ 
            $querystring = str_replace('SELECT ', 'SELECT SQL_CALC_FOUND_ROWS ', $querystring); 
        } 
        // Build query for return 
        $offset_limit = ($page_number * $num_rows_to_return); 
        $start_time = explode(' ', microtime()); 
        $start_time =  $start_time[1] + $start_time[0]; 
        $get_results = mysql_query($querystring." LIMIT ".$offset_limit.", ".$num_rows_to_return."") or die(mysql_error()); 
        $micro_time = explode(' ', microtime());   
        $query_execution_time = $micro_time[0] +  $micro_time[1] - $start_time; 
        if($dev_mode == 1){ 
        $mysql_queries_debugging .= ' 
    Query: '.$querystring." LIMIT ".$offset_limit.", ".$num_rows_to_return.' 
    Time Taken: '.round($query_execution_time, 5).' 
    '; 
    } 
        // Get total number of rows 
        $start_time = explode(' ', microtime()); 
        $start_time =  $start_time[1] + $start_time[0]; 
        $count_rows_query = mysql_query("SELECT FOUND_ROWS();"); 
        $micro_time = explode(' ', microtime());   
        $query_execution_time = $micro_time[0] +  $micro_time[1] - $start_time; 
        if($dev_mode == 1){ 
        $mysql_queries_debugging .= ' 
    Query: '."SELECT FOUND_ROWS();".' 
    Time Taken: '.round($query_execution_time, 5).' 
    '; 
    } 
        $count_rows = mysql_fetch_array($count_rows_query); 
        // Calculate the total pages we will have 
        $total_pages = ceil($count_rows[0] / $num_rows_to_return); 
        $total_pages = $total_pages - 1; 
        $total_results_feedback = $count_rows[0]; 
        // Build page links section 
        $prev_link_page = $page_number - 1; 
        $next_link_page = $page_number + 1; 
        if ($prev_link_page < 0){ 
            $prev_link_page = 0; 
        } 
        if ($next_link_page > $total_pages){ 
            $next_link_page = $total_pages; 
        } 
        $middle_page_links = ''; 
        $pages_start = ($page_number - 3) + 1; 
        if ($pages_start < 1){ 
            $pages_start = 1; 
        } 
        $count_to = $pages_start + 6; 
        if ($count_to > ($total_pages + 1)){ 
            $count_to = ($total_pages + 1); 
        } 
        $first_mid_link = ''; 
        $last_mid_link = ''; 
        for ($counter = $pages_start; $counter <= $count_to; $counter += 1) { 
            $page_link = $counter - 1; 
            if ($counter != ($page_number + 1)){ 
                $middle_page_links .= '<a href="'.$base_url.'page:'.$page_link.'/">'.$counter.'</a>'; 
                if ($counter < $count_to){ 
                    $middle_page_links .= ' &middot; '; 
                } 
                if($first_mid_link == ''){ 
                        $first_mid_link = $page_link; 
                    } 
                $last_mid_link = $page_link; 
            } else { 
                $middle_page_links .= ' <strong>'.$counter.'</strong>'; 
                if ($counter < $count_to){ 
                    $middle_page_links .= ' &middot; '; 
                } 
            } 
        } 
        if($page_number == 0){ 
            $first_link = 'First &middot; Prev | '; 
            $first_dots = ''; 
        } else { 
            $first_link = '<a href="'.$base_url.'page:0/">First</a> &middot; <a href="'.$base_url.'page:'.$prev_link_page.'/">Prev</a> | '; 
            if($page_number > 3){ 
                if($page_number != 1){ 
                    $first_dots = ' <a href="'.$base_url.'page:0/">1</a> ... '; 
                } 
            } else { 
                $first_dots = ''; 
            } 
        } 
        if($page_number == $total_pages){ 
            $last_link = ' | Next &middot; Last'; 
            $last_dots = ''; 
        } else { 
            $last_link = ' | <a href="'.$base_url.'page:'.$next_link_page.'/">Next</a> &middot; <a href="'.$base_url.'page:'.$total_pages.'/">Last</a>'; 
            if($last_mid_link <= ($total_pages - 1) || $last_mid_link != $total_pages){ 
                $last_dots = ' ... <a href="'.$base_url.'page:'.$total_pages.'/">'.($total_pages + 1).'</a> '; 
            } else { 
                $last_dots = ''; 
            } 
        } 
        $output_page_link = '<div>'.$first_link.$first_dots.$middle_page_links.$last_dots.$last_link.'</div>'; 
    
        if($total_pages == -1){ 
            $output_page_link = '<div>First &middot; Prev |  <strong>1</strong> | Next &middot; Last</div>'; 
        } 
         
        $pagination_array = Array ( 
            "page_links" => $output_page_link, 
            "results" => $get_results, 
            "total_pages" => ($total_pages + 1), 
            "total_results" => $total_results_feedback 
        ); 
        return $pagination_array; 
    }     
    
    ?> 
    PHP:

    This is my code I created to pull data from my database (experimental database, just to test out stuff and to learn). The result of my code below can be found on this page


    <?php
    
    
    $db_host = "...";
    $db_user = "smetten";
    $db_pwd = "Tjoene1";
    $db_name = "smetten";
    
    mysql_connect($db_host, $db_user, $db_pwd);
    mysql_select_db($db_name);
    
    
    $query= "SELECT * FROM contact WHERE name = 'Nick Desmedt'";
    $result = mysql_query($query);
    
    
    while($row = mysql_fetch_assoc($result))
    {
        echo "Name :{$row['name']} <br>" .
             "Subject : {$row['subject']} <br>" . 
             "Message : {$row['message']} <br><br>";
    } 
    
    ?> 
    
    PHP:
     
    smetten, Apr 29, 2008 IP
  2. saurabhj

    saurabhj Banned

    Messages:
    3,459
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you going to pay for it?
     
    saurabhj, Apr 29, 2008 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    pagination is already posted the recent topics...

    anyways heres the main idea,
    mysql_query("select * from your_table limit 0,5");

    every page increments the value of limit
    ie.
    page 1: select * from your_table limit 0,5
    this query shows records 1 up to 5
    page 2: select * from your_table limit 5,5
    this one shows records from 6 to 10

    hope this gives you an idea
     
    bartolay13, Apr 29, 2008 IP
  4. adrevol

    adrevol Active Member

    Messages:
    124
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #4
    adrevol, Apr 29, 2008 IP
  5. lanmonkey

    lanmonkey Active Member

    Messages:
    549
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #5
    The webpage has a BIG bug in in the first part of the code:

    1. get the total no of rows for the search query
    
    $query2=” SELECT * FROM pagingstu  “;
    $result2=mysql_query($query2);
    echo mysql_error();
    $nume=mysql_num_rows($result2);
    PHP:
    They are copying the contents of the entire "pagingstu" table to the variable $result2. if that table is large (or ever grows to be large) you will hit the php memory limit and your script will crash and burn.

    better to do the count in the DB query like this:
    SELECT count( * ) AS num FROM db_table
     
    lanmonkey, May 1, 2008 IP
  6. adrevol

    adrevol Active Member

    Messages:
    124
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #6


    Thats perfect, sorry for that, and thanks for the same i will change that part of the code ..

    great observing dear,
     
    adrevol, May 1, 2008 IP
  7. lanmonkey

    lanmonkey Active Member

    Messages:
    549
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Now you have changed your code you have introduced another bug....

    The first portion of your code is:

    $query2=” SELECT count(*) FROM pagingstu  “;
    $result2=mysql_query($query2);
    echo mysql_error();
    $nume=$result2
    PHP:
    …and later in your script you use $nume like so:

    if ($start1<($nume/$limit))
    PHP:
    at this point $nume is not a number, its a mysql resourse because in the first portion of your code it $nume = $result2 and $result2 = a DB query.

    from the php manual:
    To make it work how you want you could do something like:
    $nume = mysql_fetch_assoc ($result2);

    Then the value of the mysql count would be in the variable $nume['num']

    more info here: http://uk3.php.net/manual/en/ref.mysql.php
     
    lanmonkey, May 2, 2008 IP