Ashoar Posted April 10, 2009 Share Posted April 10, 2009 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? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 10, 2009 Share Posted April 10, 2009 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. Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 10, 2009 Author Share Posted April 10, 2009 Yes, but that query worked perfectly fine until the pagination was added. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 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. Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 10, 2009 Author Share Posted April 10, 2009 Tried to echo the query but i just get the same errors. Am i using the pagination correctly? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 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. Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 10, 2009 Author Share Posted April 10, 2009 Echo did not work, just continued to give the errors without echoing the query. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 $info="SELECT * from post_reply WHERE board='$forumid' AND parentid='0' ORDER BY lastrepliedto DESC LIMIT $offset, $rowsperpage"; echo 'Query:'.$info.'<br>'; $info2=mysql_query($info) or die(mysql_error()); Did you put the ECHO there? Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 10, 2009 Author Share Posted April 10, 2009 Sure did, also tried echoing lower in the page and after the array. All places still came back with the 2 errors. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 I think we're looking in the wrong place. Try changing this... $sql = "SELECT COUNT(*) FROM post_reply"; $result = mysql_query($sql, $conn) or die('No: '.mysql_error()); list($numrows) = mysql_fetch_row($result); Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 10, 2009 Share Posted April 10, 2009 Yes, but that query worked perfectly fine until the pagination was added. could've used that info before i took my time looking things over. in future, give all circumstances, errors, etc... Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 10, 2009 Author Share Posted April 10, 2009 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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 Might seem a silly question but do you actually have a table called "post_reply"? Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 10, 2009 Author Share Posted April 10, 2009 Yes, it contains all posts and replies made within the forum. It also has about 30 entries in it, so it isn't empty. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 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! Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 10, 2009 Author Share Posted April 10, 2009 Ah yes, that sorted that error out. Now the other error is still there: Fatal error: SQL in /home/public_html/forum/board.php on line 35 Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 That the same script? Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 10, 2009 Author Share Posted April 10, 2009 Yes same script. It is the database query above what we just fixed. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 OK, I'd change the line below: $result = mysql_query($sql) or die('No: '.$mysql_error()); Whatever code you're running to catch the error isn't displaying anywhere near enough information to do any debugging. Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 10, 2009 Author Share Posted April 10, 2009 "Fatal error: Function name must be a string in /home/public_html/forum/board.php on line 35" Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 10, 2009 Share Posted April 10, 2009 That's got me lost there! Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 10, 2009 Share Posted April 10, 2009 just post your most recent script and point to line 35. Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 11, 2009 Author Share Posted April 11, 2009 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>"; ?> Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 11, 2009 Share Posted April 11, 2009 echo ' <a href="board.php?board='.$forumid.'&page='.$nextpage.'">></a> '; echo ' <a href="board.php?board='.$forumid.'&page='.$totalpages.'">>></a> '; Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 11, 2009 Author Share Posted April 11, 2009 That fixed the link structure but there is still one problem. When clicking on the "Next Page" bit, it just load for a second and brings me back to the same page. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.