Jump to content

Pagination - Errors


Ashoar

Recommended Posts

I am using pagination for the first time and have run into a few errors.

I have used the tutorial from here on php freaks.

 

Here is the code:

<?php


include "config.php";

$forumid=$_GET[board];

print "<link rel='stylesheet' href='style.css' type='text/css'>";

print "<A href='post.php?board=$forumid'>New Topic</a><br>";

$page = 1;

$size = 10;

if (isset($_GET['page'])){
    $page = (int) $_GET['page'];
}

print "
<table class='maintable'>

   <tr class='headline'><td width=15%>Topic</td>
   <td width=5%>Author</td><td width=5%>Replies</td>
   <td width=20%>Last reply</td>
   </tr>
"; 


$sql = "SELECT COUNT(*) FROM post_reply";  
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);  
$r = mysql_fetch_row($result);  
$numrows = $r[0];  
  
// number of rows to show per page  
$rowsperpage = 10;  
// find out total pages  
$totalpages = ceil($numrows / $rowsperpage);  

// get the current page or set a default  
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {  
// cast var as int  
$currentpage = (int) $_GET['currentpage'];  
} else {  
// default page num  
$currentpage = 1;  
} // end if  
  
// if current page is greater than total pages...  
if ($currentpage > $totalpages) {  
// set current page to last page  
$currentpage = $totalpages;  
} // end if  
// if current page is less than first page...  
if ($currentpage < 1) {  
// set current page to first page  
$currentpage = 1;  
} // end if  

// the offset of the list, based on current page   
$offset = ($currentpage - 1) * $rowsperpage;  

$info="SELECT * from post_reply WHERE board='$forumid' AND parentid='0' ORDER BY lastrepliedto DESC LIMIT $offset, $rowsperpage";
$info2=mysql_query($info) or die(mysql_error());

while($info3=mysql_fetch_array($info2))

{

  $info3[title]=strip_tags($info3[title]);

  $info3[author]=strip_tags($info3[author]);


  print "
  <tr class='mainrow'><td><A href='message.php?id=$info3[postid]'>$info3[title]</a></td>
  <td><center><A href='member_profile.php?username=$info3[author]'>$info3[author]</a></center></td>
  <td><center>$info3[numreplies]</center></td>
  <td>$info3[showtime]<br>Last post by <A href='member_profile.php?username=$info3[lastposter]'>$info3[lastposter]</a></td>
  </tr>
  ";
}

/******  build the pagination links ******/  
// range of num links to show  
$range = 3;  

// if not on page 1, don't show back links  
if ($currentpage > 1) {  
// show << link to go back to page 1  
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";  
// get previous page num  
$prevpage = $currentpage - 1;  
// show < link to go back to 1 page  
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";  
} // end if   
  
// loop to show links to range of pages around current page  
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {  
// if it's a valid page number...  
if (($x > 0) && ($x <= $totalpages)) {  
// if we're on current page...  
if ($x == $currentpage) {  
// 'highlight' it but don't make a link  
echo " [<b>$x</b>] ";  
// if not current page...  
} else {  
// make it a link  
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";  
} // end else  
} // end if   
} // end for  
           
// if not on last page, show forward and last page links      
if ($currentpage != $totalpages) {  
// get next page  
$nextpage = $currentpage + 1;  
// echo forward link for next page   
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";  
// echo forward link for lastpage  
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";  
} // end if  
/****** end build pagination links ******/ 
print "</table>";

?>  

 

Here are the errors:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource (line 35)

 

Fatal error: SQL (line 35)

 

What am i doing wrong?

Link to comment
Share on other sites

i know this is a PHP forum, but typing "Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource" into Google returned 1,850,000 results in 0.23 seconds.

 

took me longer than 0.23 seconds to write this reply, that's for sure.

 

it means that something in your query is incorrect.

 

check $conn for accuracy.

Link to comment
Share on other sites

This the line causing the error?

$info="SELECT * from post_reply WHERE board='$forumid' AND parentid='0' ORDER BY lastrepliedto DESC LIMIT $offset, $rowsperpage";

 

Why not ECHO the query to the browser so you can see what it actually looks like - that's more likely to be the quickest way to diagnose the problem.

Link to comment
Share on other sites

Yes, you'll get the same error but at least you'll be able to SEE EXACTLY what is in the query.

 

If the query ran OK before you tried to pageinate it then that tells me the LIMIT values aren't quite right.

 

Try ECHOing it to the browser and posting what you get here.

Link to comment
Share on other sites

Ok this time only the 1 error was displayed:

"Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource"

 

And the part we just changed gave the output "No: "

So it seems a MYSQL error on that part.

 

Link to comment
Share on other sites

I should learn to read the error messages properly...

not a valid MySQL-Link resource

This is actually saying that $conn isn't a valid connection - make sure you've got a valid connection to the database and try again. If you DO have a connection then use this:

$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);  
list($numrows) = mysql_fetch_row($result);

 

MrMArcus had it sussed at a very early stage - well done!

Link to comment
Share on other sites

Ok i have the main part of it working now.

 

Now there is a few problems.

The link for the next page does not work correctly.

It is linked like this: /board.php?board=test&page=2>></a> <a href =

 

If i copy that into the address bar and remove those end bits it just directs me to the page i am already on.

Here is this code:

 

<?php


include "config.php";

$forumid=$_GET[board];

print "<link rel='stylesheet' href='style.css' type='text/css'>";

print "<A href='post.php?board=$forumid'>New Topic</a><br>";

$page = 1;

$size = 10;

if (isset($_GET['page'])){
    $page = (int) $_GET['page'];
}

print "
<table class='maintable'>

   <tr class='headline'><td width=15%>Topic</td>
   <td width=5%>Author</td><td width=5%>Replies</td>
   <td width=20%>Last reply</td>
   </tr>
"; 


$sql = "SELECT COUNT(*) FROM post_reply";  
$result = mysql_query($sql) or die('No: '.$mysql_error());
list($numrows) = mysql_fetch_row($result);
$numrows = $r[0];  
  
// number of rows to show per page  
$rowsperpage = 10;  
// find out total pages  
$totalpages = ceil($numrows / $rowsperpage);  

// get the current page or set a default  
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {  
// cast var as int  
$currentpage = (int) $_GET['currentpage'];  
} else {  
// default page num  
$currentpage = 1;  
} // end if  
  
// if current page is greater than total pages...  
if ($currentpage > $totalpages) {  
// set current page to last page  
$currentpage = $totalpages;  
} // end if  
// if current page is less than first page...  
if ($currentpage < 1) {  
// set current page to first page  
$currentpage = 1;  
} // end if  

// the offset of the list, based on current page   
$offset = ($currentpage - 1) * $rowsperpage;  

$info="SELECT * from post_reply WHERE board='$forumid' AND parentid='0' ORDER BY lastrepliedto DESC LIMIT $offset, $rowsperpage";
$info2=mysql_query($info) or die(mysql_error());

while($info3=mysql_fetch_array($info2))

{

  $info3[title]=strip_tags($info3[title]);

  $info3[author]=strip_tags($info3[author]);


  print "
  <tr class='mainrow'><td><A href='message.php?id=$info3[postid]'>$info3[title]</a></td>
  <td><center><A href='member_profile.php?username=$info3[author]'>$info3[author]</a></center></td>
  <td><center>$info3[numreplies]</center></td>
  <td>$info3[showtime]<br>Last post by <A href='member_profile.php?username=$info3[lastposter]'>$info3[lastposter]</a></td>
  </tr>
  ";
}

/******  build the pagination links ******/  
// range of num links to show  
$range = 3;  

// if not on page 1, don't show back links  
if ($currentpage > 1) {  
// show << link to go back to page 1  
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";  
// get previous page num  
$prevpage = $currentpage - 1;  
// show < link to go back to 1 page  
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";  
} // end if   
  
// loop to show links to range of pages around current page  
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {  
// if it's a valid page number...  
if (($x > 0) && ($x <= $totalpages)) {  
// if we're on current page...  
if ($x == $currentpage) {  
// 'highlight' it but don't make a link  
echo " [<b>$x</b>] ";  
// if not current page...  
} else {  
// make it a link  
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";  
} // end else  
} // end if   
} // end for  
           
// if not on last page, show forward and last page links      
if ($currentpage != $totalpages) {  
// get next page  
$nextpage = $currentpage + 1;  
// echo forward link for next page   
echo " <a href='board.php?board=$forumid&page=$nextpage>></a> ";  
// echo forward link for lastpage  
echo " <a href='board.php?board=$forumid&page=$totalpages'>>></a> ";  
} // end if  
/****** end build pagination links ******/ 
print "</table>";

?>  

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.