Jump to content

Recommended Posts

Hi, i ws hoping for a little help with my php if possible.

 

I have created a guestbook page that pulls records from a database and displays them, along with the PREV and NEXT links (& page numbers between them).

 

All seems to be worknig correctly except for 2 things!!........

 

Firstly the NEXT link text is hyperlinked even whn the user is at the last page! so they end up able to continuously being able to go to the next page, even though there are no records!

 

Secondly the page numbers (in between PREV NEXT) doont seem to go up enough to the amount of pages worth of records!! I have tested knowing that there are 5 pages worth of records and when i get to page 4....there is no page 5 displayed...if i click NEXT however,  iam taken to the next set of records (page 5) which 's page number was not displayed!!!!

 

 

I have tried to do some debugging to do with the code refering to the NEXT hyperlink and after echoing out to see whast was working or not i found that the variable $totalrows which is associated with a record_count query always would return the value of 1???? Even when there were clearly 21 rows worth of data there!!!!!????? Could this be part ofthe problem???

 

These are the to areas that im really stuck on, i could use and im  at my witz end now

 

Please help?!!

 

Thank You

 

code below:

 


<?php
$dbservertype='mysql';
$servername='localhost';
// username and password to log onto db server
$dbusername='root';
$dbpassword='';
// name of database
$dbname='lopesarms';


connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
  error_reporting(E_ALL);
ini_set('display_errors', '1'); 

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

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

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM guest_book LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error()); 
echo"$totalrows";
    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["name"]);
    
    echo($row["email"]);
    echo("</td></tr>");
    }

    echo("</table>");

  echo"<font size=2>";
$pageprev = $page-1;

    if($page != 1){ 
       
        


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

    $numofpages =  $limit / $totalrows; 
    

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



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

//$new = ($limit * $page) - $totalrows;
//echo"$new";

    if(($limit * $page) - ($totalrows) > 0){
       
         



        echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>");

    }else{
        echo("NEXT"); 

    }
    echo"</font>";
    mysql_free_result($result);

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/69124-solved-little-pagnation-help-please/
Share on other sites

try

<?php
$dbservertype='mysql';
$servername='localhost';
// username and password to log onto db server
$dbusername='root';
$dbpassword='';
// name of database
$dbname='lopesarms';


connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
  error_reporting(E_ALL);
ini_set('display_errors', '1'); 

    $limit          = 4;               
    $query_count    = "SELECT count(*) FROM guest_book";    
    $result_count   = mysql_query($query_count);    
    //$totalrows      = mysql_num_rows($result_count);   <-- change this line 
    $totalrows      = mysql_result($result_count, 0, 0); 
    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM guest_book LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error()); 
echo"$totalrows";
    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["name"]);
    
    echo($row["email"]);
    echo("</td></tr>");
    }

    echo("</table>");

  echo"<font size=2>";
$pageprev = $page-1;

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

    //$numofpages =  $limit / $totalrows;          <-- change this line
    $numofpages =  ceil($totalrows /$limit);

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


/* remove this part
    if(( $limit % $totalrows) != 0){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }
*/
$pagenext = $page+1;

//$new = ($limit * $page) - $totalrows; <-- and remove this part
//echo"$new";

    if($page <= $numofpages) {
        echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>");
    }else{
        echo("NEXT"); 
    }
    echo"</font>";
    mysql_free_result($result);
?>

Hi,

 

there is just one thing that im a little unsure of though....

 

The NEXT hyperlink now disappears as it should do at the end of the page. But only 1 page after the last page....???

 

For example i when there are only 6 pages worth of records, when on page 6 the NEXT hyperlink is still there....???? If its clicked your taken to page 7's worth of records (which is empty as there are none).  But then the NEXT pagnation text is NOT hyperlinked!!

 

So it is working almost perfeclty but not quite.

 

Any help me greatly appreciated, especially you sasa coz your a genious :)!

 

Thank you

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.