Jump to content

[SOLVED] Pagination Help


RealDrift

Recommended Posts

HI,

 

I read a tutorial on how to turn my results query from the db into pages for easy user reading. It all worked out. The table was echoed with results. However the links "PREV" and "NEXT" do not work, they are unclickable.

 

Between these two links should be the pages in a row, i know for a fact that my query would produce around 4 pages yet the result page lists only one.

 

I am currently developing the website on my pc using XAMPP, just so you know.

 

I have tried fixing this problem and have failed. Please could someone solve this problem, i would highly appreciate it.

 

  <?php

    $limit          = 7;               
    $query_count    = "SELECT count(*) FROM goldies WHERE Sender='$username'";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count); 

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

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

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

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

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

    echo("<tr bgcolor=".$bgcolor."><td>");
    echo($row["Receiver"]);
    echo("</td><td>");
echo($row["Amount"]);
    echo("</td><td>");
    echo($row["Date"]);
    echo("</td></tr>");
    }

    echo("</table>");

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


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

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

?> 

Link to comment
Share on other sites

  • Replies 126
  • Created
  • Last Reply

Top Posters In This Topic

    $limit          = 7;               
    $query_count    = "SELECT count(*) FROM goldies WHERE Sender='$username'";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count); 

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

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $limitvalue, $limit"; 

 

check the $limit in the select statement i believe that is wrong because that should adjust or increment 7 when pages goes up

 

Link to comment
Share on other sites

First of all, you just need to add some debugging information: i.e. have the script echo variables and results to the page so you can verify what is happening in the code.

 

For example, had you you echo'd $totalrows to the page you would see that it is always equal to 1.

 

The problem is that your query will always return a single record with the value in that record being the number of rows. Then you use mysql_num_rows() on that (and there is only one record in the result). Either have your query return a result set of all the records and use mysql_num_rows OR use COUNT() and then get the value from that result set:

 

    $query_count    = "SELECT count(*) FROM goldies WHERE Sender='$username'";    
   $result_count   = mysql_query($query_count);    
   $totalrows      = mysql_result ($result_count, 0, 0);

Link to comment
Share on other sites

Your links are not created properly! The first one uses an ampersand for the first paramater and it should be a question mark. Plus both should be using forward slashes, not backslashes.

 

i fixed the slashes and removed the "game" directory out but don't know what you mean by ampersand thungy

Link to comment
Share on other sites

Your links are not created properly! The first one uses an ampersand for the first paramater and it should be a question mark. Plus both should be using forward slashes, not backslashes.

 

i fixed the slashes and removed the "game" directory out and removed the "&" sign and put in "?" but still not working

 

Link to comment
Share on other sites

Go look at the tutorial teng84 posted and rread through it. It is a very good tutorial.

 

I'll give you ONE more freebie:

 

This

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

 

Should be

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

 

However, you should be doing some validation to ensure that the passed value is of the orrect type and value.

Link to comment
Share on other sites

This whole script was from a tutorial from this very website, so i assumed it would be correct. If i knew it had this many problems i would have left it alone.

 

I am not a super coder, my php coding level is around intermediate. I can't do this on my own. I had been staring at this script for like an hour before i came here.

Link to comment
Share on other sites

This whole script was from a tutorial from this very website, so i assumed it would be correct. If i knew it had this many problems i would have left it alone.

 

I am not a super coder, my php coding level is around intermediate. I can't do this on my own. I had been staring at this script for like an hour before i came here.

 

The links work in a way now. The page number links do work, however the current page number should not be clickable as you are already veiweng that page, but now the current page is clickable but the page number before current page number is not clickable.

 

And the next and prev links work but the other way around. "NEXT" actually takes you to the previous page and vice versa

Link to comment
Share on other sites

Post your complete updated code.

 

<?php

    $limit          = 10;               
    $query_count    = "SELECT count(*) FROM goldies WHERE Sender='$username'";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_result ($result_count, 0, 0);

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

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

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

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

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

    echo("<tr bgcolor=".$bgcolor."><td>");
    echo($row["Receiver"]);
    echo("</td><td>");
echo($row["Amount"]);
    echo("</td><td>");
    echo($row["Date"]);
    echo("</td></tr>");
    }

    echo("</table>");

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

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


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

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

?>

Link to comment
Share on other sites

To be honest, it's nearly impossible (for me at least) to help people with pagination problems. I remember first doing pagination and it took me a while to find a tutorial that worked out right. So I just kept trying different ones, and finally I got one.

 

I suggest searching Google again and keep on trying different tutorials out.

 

This one looks good

http://php.about.com/od/phpwithmysql/ss/php_pagination.htm

Link to comment
Share on other sites

Ok, tried other script:

 

<?php

//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM goldies WHERE Receiver='$username'") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 4;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM goldies WHERE Receiver='$username' LIMIT $max") or die(mysql_error());

//This is where you display your query results
while($info = mysql_fetch_array( $data_p ))
{
echo("<tr bgcolor=".$bgcolor."><td>");
    echo($info["Receiver"]);
    echo("</td><td>");
echo($info["Amount"]);
    echo("</td><td>");
    echo($info["Date"]);
    echo("</td></tr>");
}
echo "<p>";

// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>

 

I get the error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit -4,4' at line 1

 

i think it relates to the SQL queires and their limit function

Link to comment
Share on other sites

Okay, try changing this part of your code

 

<?php

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM goldies WHERE Receiver='$username' LIMIT $max") or die(mysql_error());

?>

 

To

<?php

//This is your query again, the same one... the only difference is we add $max into it
$data_q = "SELECT * FROM goldies WHERE Receiver='$username' LIMIT $max";
$data_p = mysql_query($data_q)or die(mysql_error().'<p>With query:<br>'.$data_q);

?>

 

Post what it gives you

Link to comment
Share on other sites

Here's a modification of your previous code. Give it a try:

 

NOTE: One thing that is not handled here is how $username is set. I would expect that it should be included on the query string as well.

 

<?php

    $limit          = 10;               
    $query_count    = "SELECT count(*) FROM goldies WHERE Sender='$username'";    
    $result_count   = mysql_query($query_count) or die("Error: " . mysql_error());    
    $totalrows      = mysql_result ($result_count, 0, 0);
    $numofpages     = ceil($totalrows/$limit); 

    $page = (empty($_GET['page']) || !is_int($_GET['page']))?1:abs($_GET['page']);

    $limitvalue = ($page-1) * $limit;
    $query  = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

    //Exit if no records to display
    if(mysql_num_rows($result) == 0){
        exit("Nothing to Display!");
    }

    //Create the table of output data
    echo "<table>\n";
    while($row = mysql_fetch_array($result)){
        //Alternate the row background color
        $bgcolor = ($bgcolor == "#737373")?'#737373':'#E0E0E0';
        //Display the row of data
        echo "<tr bgcolor=\"$bgcolor\">\n";
        echo "  <td>".$row["Receiver"]."</td>\n";
        echo "  <td>".$row["Amount"]."</td>\n";
        echo "  <td>".$row["Date"]."</td>\n";
        echo "</tr>";
    }
    echo "</table>\n";

    //Enable the Prev link if not first page
    if($page > 1){ 
        echo("<a href=\"creditshistory.php?page=".($page-1)."\">PREV</a> "); 
    }else{
        echo("PREV ");
    }

    //Create links for each page in report
    for($i=1; $i<=$numofpages; $i++){
        if($i == $page){
            echo "$i ";
        }else{
            echo "<a href=\"creditshistory.php?page=$i\">$i</a> ";
        }
    }

    //Enable the Next link if not last page
    if($page < $numofpages){
        echo("<a href=\"creditshistory.php?page=".($page+1)."\">NEXT</a>"); 
    }else{
        echo("NEXT"); 
    }
    
    mysql_free_result($result);

?>

Link to comment
Share on other sites

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.