Jump to content

Pagination Links not Working


webguync

Recommended Posts

From the tutorial on pagination from this site I have the following code, but don't get links only text. What do I have wrong?

 

<?php
if($page != 1){  
        $pageprev = $page--; 
         
        echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");  
    }else{ 
        echo("PREV".$limit." "); 
    } 

    $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); 
    ?>

 

this produced PREV25 1 NEXT25 without links

Link to comment
https://forums.phpfreaks.com/topic/55799-pagination-links-not-working/
Share on other sites

Yhere you go a little test for your database ok.

<?php

//database connection.

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

$max_results = 1; 

$from = (($page * $max_results) - $max_results); 

//database query.

$query="select * from what_ever  DESC LIMIT $from, $max_results ";

$result=mysql_query($query);

while($rec=mysql_fetch_assoc($result)){

$point_query="select * from blogs where what_ever='what_evrer' ";

$point_result=mysql_query($point_query)or die(mysql_error());

while($p=mysql_fetch_assoc($point_result)){

// show the information	

}

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM what_ever"),0); 

if($page > 1){ 
    
$prev = ($page - 1); 
  
echo "<div align='center'><a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">Prev</a>"; 

} 

for($i = 1; $i <= $total_pages; $i++){ 
    if(($page) == $i){ 
        echo "<font color='yellow'>$i</font> "; 
        } else { 
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\"></a> "; 
    } 
} 

if($page < $total_pages){ 
    $next = ($page + 1); 

// show other infomation.

} 
}
?>

the limiting of records is working, but I am still not getting the Next and Previous records to work. The end result I would like is to display 25 records on a page, and then have a link, to the next 25, and backo the previous page (if there is one).

 

Here is my current code

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

//check for validity of user
$db_name="people";
$table_name ="Listing";
$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>";
?>
<html>
<head>
<title>MySQL Results Display</title>
</head>
<body>

<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); 
    ?>
<p><a href="Recruiters.php"> Return to main menu</a></p>

</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.