Zend - The PHP Company




Databases

Add Code


MySQL  

Type: code fragment
Added by: balajir
Entered: 06/02/2004
Last modified: 02/12/2003
Rating: - (fewer than 3 votes)
Views: 6807
fuction to display next, previous links for a query and query is returned with limits


<?php
//write code for db connection.. 

$query 'SELECT * FROM table'

/* just call the function display_page to show next, previous links.. it print the links and return the query with limits*/ 

$query display_page($query); 

/* write code to display the result using the new query */ 

$link mysql_query($query); 

while (
$res mysql_fetch_array($link)) { 
   
//code to display ouput 
   



/**----------------------------------------------------------------------------------
 *fuction: display_page 
 *author : balaji r. 
 *Display next, previous links for a query
 * 
 * @param $query. enter a valid query
 * @param $max. no of results to display
 * @return - $query with limits
 **/
function display_page($query$max=10)
{
    
$link mysql_query($query);
   
    if (
$total mysql_num_rows($link)) { //if records found

        
($page_number $_GET['page_number']) or ($page_number 1);

        
$start = ($page_number 1) * $max;
        
$end $start $max;
        
$total_page ceil($total/$max);
        
        
$query_string str_replace("page_number=$page_number"''$_SERVER['QUERY_STRING']); 
        
$query_string and $query_string .= '&'//add querystring to url

        
print "$total Record(s) Found. Displaying the Page $page_number out of $total_page. <br>";

        if (
$end $total ) { //show next link
            
$link_next $page_number 1;
            print 
"<a href='?{$query_string}page_number=$link_next'>Next</a>&nbsp";
        }

        if (
$end $max) { //show previous link
            
$link_previous $page_number 1;
            print 
"<a href='?{$query_string}page_number=$link_previous'>Previous</a>";
        }
        
$query .= " LIMIT $start$max"
    }
    else {
        print 
'No Record to display.<br>';
    }
    return 
$query;
}
// end of function
?>


Usage Example




Rate This Script





Search



This Category All Categories