Jump to content

[SOLVED] Pagination Help


RealDrift

Recommended Posts

poco i get same error still:

 

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 '-4,4' at line 1

 

With query:

SELECT * FROM goldies WHERE Receiver='WeedHunteR' LIMIT -4,4

Link to comment
Share on other sites

  • Replies 126
  • Created
  • Last Reply

I think it's because your getting a negative number (-4).

 

Try changing

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

 

To

<?php

$first_l = ($pagenum - 1) * $page_rows;

if ($first_l < 0) $max = '0, '.$page_rows;
else $max = $first_l .', ' .$page_rows;

?>

Link to comment
Share on other sites

ok the table is echoed now, although its wide and the links are at the top of the page in the top left hand corner when i hoped they would have been under the table,

 

the links NEXT and LAST have the right urls but when i click them the page reloads page 1 again.

Link to comment
Share on other sites

damato,

 

i changed the code you gave me and i got the following results:

 

When i hover mouse over NEXT link i see the link in status bar for page 2 which is correct. But when i click any link it only reloads page 1.

 

What EXACTLY is the URL when you hover over the next link? Have you tried to verify what $page is getting set to?

Link to comment
Share on other sites

poco, the script you told me to use from about.com uses pagenum, however the script me and damato were working on uses page.

 

My earlier comment about the URL was for his script, the script me and you were looking at is:

 

<?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 = 10;

//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
$first_l = ($pagenum - 1) * $page_rows;

if ($first_l < 0) $max = '0, '.$page_rows;
else $max = $first_l .', ' .$page_rows;


//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);

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

// 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> ";
}
?>

 

This uses pagenum. This produces the table okay and everything, but when i click the links the page reloads to show page 1. The Links have the correct urls however.

 

My earlier post mentioning urls having 'page' links was for the script i am working on with damato.

 

Is it all clear?

Link to comment
Share on other sites

basically teng the problem is, the pagination displays ok. Everything works as in the table with results is displayed. The links to different pages is also displayed for navigation.

 

Only problem is when i click the links to go to different pages the whole page reloads and still only reloads to show page 1. I cannot navigate to other pages.

 

Me and damato were looking at a seperate pagination script and me and poco were looking at another. Both display everything fine but in both the links do not seem to work. They just reload page 1 of results again.

 

Below is the code:

 

<?php

$username=$_SESSION['username'];
    $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':'#737373';
        //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

<?php

$username=$_SESSION['username'];
    $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;
$limitrange = $limitvalue+$limit;
    $query  = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $limitvalue, $limitrange ";        
    $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':'#737373';
        //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);

?>




[code]

ok try ..

[/code]

Link to comment
Share on other sites

i think im getting closer..

<?php

$username=$_SESSION['username'];
    $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;
$limitrange = $limitvalue+$limit;
    $query  = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $limitvalue, $limitrange ";   
echo  $limitvalue.'<br>';    
echo  $limitrange.'<br>';    
echo  $query .'<br>';    
    $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':'#737373';
        //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);

?>


try that and give the result if the echo i made from first page and upon next navigation

Link to comment
Share on other sites

urgh i am so tired of this, i know, its a lot of headache just for numbering some pages

 

and yes i do see page=2

 

let me post the whole code including html, maybe its something wrong there:

 

<?
include "includes/functions.php";
include "includes/db_connect.php";

session_start();
logincheck();
$username=$_SESSION['username'];

echo "<link rel=stylesheet href=includes/in.css type=text/css>";

?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style19 {
font-size: 11px;
font-weight: bold;
}
.style20 {
font-size: 10px;
font-weight: bold;
color: #FFFFFF;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
-->
</style>
</head>

<body>
  <table width="80%" border="0" align="center" cellspacing="5">
    <tr>
      <td colspan="4"><div align="center" class="style19">History </div><br>        <table width="100%" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#000000" bordercolorlight="#000000" bordercolordark="#000000" rules= class=thinline>
          
          
          
          
          
        </table></td>
    </tr>
    
    <tr>
      <td width="294" height=""> <?php

$username=$_SESSION['username'];
    $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;
$limitrange = $limitvalue+$limit;
    $query  = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $limitvalue, $limitrange ";   
echo  $limitvalue.'<br>';    
echo  $limitrange.'<br>';    
echo  $query .'<br>';    
    $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':'#737373';
        //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);

?> </td>
      <td width="17"> </td>
      <td width="273"> </td>
    </tr>
  </table>

</body>
</html>

Link to comment
Share on other sites

I had this same problem, and true enough it was my $page variable that was acting weird. I change it to $_GET['page'] and funny enough it worked just fine.

 

Try it.

 

or

 

change

 

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

 

to

 

//Enable the Next link if not last page

echo "page number : ". $page "<br>";
echo "number of pages : ". $numofpages;


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

 

see what the numbers are

Link to comment
Share on other sites

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.