Jump to content

Pagination help please


Mr Chris

Recommended Posts

Hi,

I’m trying to put together a pagination script in php.  The records get called out, but the next / previous buttons don’t <a href> despite there being over 25 entries and me asking the code to <a href>?

I’m hoping it’s something blatently obvious someone can tell me!

Many Thanks

Chris

[code=php:0]
<?php

    include('************');
    $can_i_connect = db_connect(); // by db_connect function is in my include file
   
    if(!$can_i_connect)
        {
        echo "Could not connect to database";
        }

    $limit          = 10;               
    $query_count    = "SELECT count(*) FROM directory_listings";   
    $result_count  = mysql_query($query_count);   
    $totalrows      = mysql_num_rows($result_count); 

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

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM directory_listings LIMIT $limitvalue, $limit";       
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    $bgcolor = "#E0E0E0"; // light gray

    echo("<table>");
   
    while($row = mysql_fetch_array($result)){
        if ($bgcolor == "#E0E0E0"){
            $bgcolor = "#FFFFFF";
        }else{
            $bgcolor = "#E0E0E0";
        }

    echo("<tr bgcolor=".$bgcolor."><td>");
    echo($row["company_name"]);
    echo("</td><td>");
    echo($row["town"]);
    echo("</td></tr>");
    }

    echo("</table>");

    if($page != 1){ 
        $pageprev = $page--;
       
        echo("<a href=\"test.php&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=\"test.php?page=$i\">$i</a> ");
        }
    }


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

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

?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/28220-pagination-help-please/
Share on other sites

[code]
$query_count    = "SELECT count(*) FROM directory_listings";   
$result_count  = mysql_query($query_count);   
$totalrows      = mysql_num_rows($result_count);
[/code]
You using the aggregrate COUNT in the select statement which will returns one row with the value of count.

Use the following to just get the count value:-
[code]
$query_count    = "SELECT count(*) FROM directory_listings";   
$result_count  = mysql_query($query_count) or die("Error: " . mysql_error());   
$totalrows      = mysql_result($result_count, 0);
[/code]


if that was the tutorial from phpfreaks, i've noticed there was a problem with
the first query where is uses 'COUNT(*)' change....
$limit          = 10;               
$query_count    = "SELECT count(*) FROM directory_listings";

To this...
$limit          = 10;               
$query_count    = "SELECT * FROM directory_listings";   

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.