Need some assistanct getting pagination links to work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Need some assistanct getting pagination links to work

Post by bruceg »

Hello,

I have a page which displays 25 records from a MySQL database on a page, and I want to have a link to the next 25 records, and a link back to the previous 25 records when that is available. My links as I have the code so far aren't working.

Any offers of assistance would be greatly appreciated!
Here is the PHP code:

Code: Select all

<?php
//start a session
session_start();

//check for validity of user
$db_name="DB";
$table_name ="Table";
$connection = @mysql_connect("localhost", "username", "password")
or die (mysql_error());
$db = @mysql_select_db($db_name, $connection) or die (mysql_error());
$limit      = 25;                
    // Sets how many results shown per page 
     
$query_count    = "SELECT count(*) FROM $table_name";     
    // Sets what we want to pull from the database 
    
     
$result_count   = mysql_query($query_count);     
    // Pulls what we want from the database 
     
$totalrows  = mysql_num_rows($result_count);     
    // This counts the number of users 
if(empty($page)){    // Checks if the $page variable is empty (not set) 
$page = 1;      // If it is empty, we're on page 1 
} 

    $limitvalue = $page * $limit - ($limit);  
    // Ex: (2 * 25) - 25 = 25 <- data starts at 25 
//build and issue query
$sql ="SELECT id, f_name, l_name, company FROM $table_name ORDER BY company LIMIT $limitvalue, $limit";
$result = @mysql_query($sql, $connection) or die(mysql_error());
                   $limit          = 25;                
    $query_count    = "SELECT count(*) FROM $table_name";     
    $result_count   = mysql_query($query_count);     
    $totalrows      = mysql_num_rows($result_count);  

    if(empty($page)){ 
        $page = 1; 
    } 
         

    $limitvalue = $page * $limit - ($limit);  
 

    if(mysql_num_rows($result) == 0){ 
        echo("Nothing to Display!"); 
    } 
//create list block of results
$contact_list="<ul class='vertical_list'>";
while ($row = mysql_fetch_array($result)) {
         $id = $row['0'];
         $f_name=$row['1'];
         $l_name=$row['2'];
        $company=$row['3'];
         $contact_list .="<li>
        <a href=\"show_contact.php?id=$id\">$company,$l_name, $f_name, </a>";
}
$contact_list .="</li></ul>";
?>
<!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" xml:lang="en-US" lang="en-US" dir="ltr">
<head>
<meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1" />
<title>PHP Pagination</title>


</head>
<body>


<!-- begin outer wrapper div --> 

<div id="wrapper_outer">

<!-- begin header div --> 
<!-- top nav list --> 


<!-- inner content wrapper div --> 
<div id="wrapper_inner">

<div id="main_content">
<!-- old browser help -->

<p>Select a contact from the list below, to view a contact's record</p>
<? echo "$contact_list"; ?>
<?php
if(!isset($_GET['page'])){ 
    $page = 1; 
} else { 
    $page = $_GET['page']; 
}




    $numofpages = $totalrows / $limit;  
     
    for($i = 1; $i <= $numofpages; $i++){ 
        if($i == $page){ 
            echo($i." "); 
        }else{ 
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
        } 
    } 


    if(($totalrows % $limit) != 0){ 
        if($i == $page){ 
            echo($i." "); 
        }else{ 
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
        } 
    } 

    if(($totalrows - ($limit * $page)) > 0){ 
        $pagenext = $page++; 
          
        echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");  
    }else{ 
        echo("NEXT".$limit);  
    } 
     
    mysql_free_result($result); 
    ?>

</div>


</div>
</div>

</body>

</html>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You don't have $page set to $_GET['page'] until very late in your code.

I always create a function for pagination.

Code: Select all

function ShowPagination($page, $maxPages)
{
    if($page > 0) { echo '<a href="?p=' . ($page - 1) . '">Prev</a>'; }
    for($i = 0; $i < $maxPages; $i++) { echo '<a href="?p=' . ($i + 1) . '">' . ($i + 1) . '</a>'; }
    if($page < $maxPages) { echo '<a href="?p=' . ($page + 1) . '">Next</a>'; }
}
$maxPages would be equal to ciel(mysql_num_rows()/$limit).
bruceg
Forum Contributor
Posts: 174
Joined: Wed Mar 16, 2005 11:07 am
Location: Morrisville, NC
Contact:

Post by bruceg »

thanks for the reply,

so the code you provide should replace this?

Code: Select all

<?php
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}




$numofpages = $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}


if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}

if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page++;

echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");
}else{
echo("NEXT".$limit);
}

mysql_free_result($result);
?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Couldn't say. Don't feel like reading it. :-p

It's a fairly simple function, and I wrote it as I posted it to you. Modify it and use it, then ask questions. :P
Post Reply