Noskiw Posted October 18, 2009 Share Posted October 18, 2009 <?php $title = "Guestbook"; $connect = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("guest") or die(mysql_error()); function bbcode($string) { if ($string) { $bbcode_array = array('[b]', '[/b]', '[u]', '[/u]', '[i]', '[/i]', '[code]', ' ', ''); $bbcode_array_2 = array('<b>', '</b>', '<u>', '</u>', '<i>', '</i>', '<center><div style="width:90%;padding:3px;background-color:#000099;color:#FFFFFF;border:2px solid;">', '</div></center>', '<img src="', '">'); $new_string_2 = str_ireplace($bbcode_array, $bbcode_array_2, $string); return $new_string_2; } } echo "<h1>Guestbook</h1><hr />"; $queryget = mysql_query("SELECT * FROM guest ORDER BY id DESC") or die(mysql_error()); $querygetrownum = mysql_num_rows($queryget); if ($querygetrownum == 0) { echo "No posts have been made yet! Be the first!"; } //start pagination $per_page = 5; $start = $_GET['start']; $record_count = mysql_num_rows(mysql_query("SELECT * FROM guest")); $max_pages = $record_count / $per_page; if (!$start) { $start = 0; $get = mysql_query("SELECT * FROM guest ORDER BY id DESC LIMIT $start, $per_page"); while ($row2 = mysql_fetch_assoc($get)) { $name2 = $row2['name']; $email2 = $row2['email']; $message2 = $row2['message']; $date2 = $row2['date']; $time2 = $row2['time']; echo "<table><tr><td><b>Posted by: " . $name2 . "(" . $email2 . ") on " . $date2 . " at " . $time2 . "</b></td></tr><tr><td>" . nl2br(bbcode(strip_tags($message2))) . "</td></tr></table>"; echo "<hr />"; } } //setup prev and next variables $prev = $start - $per_page; $next = $start + $per_page; //show prev button if (!($start <= 0)){ echo "<a href='index.php?p=guestbook&start=$prev'>« Prev</a> "; }else{ echo "« Prev"; } //show page numbers //set variable for first page $i = 1; for ($x = 0; $x < $record_count; $x = $x + $per_page) { if ($start != $x){ echo " <a href='index.php?p=guestbook&start=$x'>$i</a> "; }else{ echo " <a href='index.php?p=guestbook&start=$x'><b>$i</b></a> "; } $i++; } //show next button if (!($start >= $record_count - $per_page)){ echo " <a href='index.php?p=guestbook&start=$next'>Next »</a>"; }else{ echo "Next »"; } if ($_POST['submit']) { $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $date = date("Y-m-d"); $time = date("H:i:s"); if ($name && $email && $message) { $querypost = mysql_query("INSERT INTO guest VALUES('','" . $name . "','" . $email . "','" . $message . "','" . $date . "','" . $time . "')"); echo "Please wait... <meta http-equiv='refresh' content='2'>"; } else { echo "Please fill out all fields!"; } } echo "<hr />"; echo " <form action='guestbook.php' method='POST'> <table width='100%'> <tr> <td width='7%' valign='top'> Your Name: </td> <td valign='top'> <input type='text' name='name' maxlength='25' /> </td> </tr> <tr> <td valign='top'> Your Email: </td> <td> <input type='text' name='email' maxlength='25' /> </td> </tr> <tr> <td valign='top'> Your Message: </td> <td> <textarea cols='20' rows='2' name='message' maxlength='250'></textarea> <p><input type='submit' name='submit' value='Post' /> </td> </tr> </table> </form>"; ?>[/code] my problem is that when i got to the next page, it doesn't show my next set of results in the mysql table... I was hoping that someone here could help me, i've tried rearanging the curly brackets but nothing seems to work... Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/ Share on other sites More sharing options...
Zyx Posted October 18, 2009 Share Posted October 18, 2009 Take a closer look at your code. The loop that reads the guestbook entries is executed only, if the $start variable equals 0 (the type is casted to boolean). If you set any other value, this code simply won't execute. Moreover, your code suffers from lots of critical problems: $record_count = mysql_num_rows(mysql_query("SELECT * FROM guest")); NEVER count the rows in this way! This is probably the slowest possible solution, because PHP must retrieve all the entries, and then count them on the script side. What is more, you do not order to read just row ID, you order to read EVERYTHING, including the content! And what if there are 10000 rows in the guestbook database? Have you thought about it? Actually, have you heard of SELECT COUNT(id) FROM table SQL syntax? Another problem: if you got the row number earlier, why you get it once again later from the database instead of using the already possesed result? $start = $_GET['start']; What is the purpose of introducing another unnecessary temporary variable? It gives you absolutely nothing. Don't you know that you can operate directly on $_GET['start']? Furthermore, if the variable is not defined, you try to acces an undefined variable which is considered as a bad practice. if(!$start){ Despite being the reason of your problems, this is incorrect for one more reason: with this code you do not actually check if the variable is set. The proper code should be: if(!isset($_GET['start'])){ $get = mysql_query("SELECT * FROM guest ORDER BY id DESC LIMIT $start, $per_page"); SQL Injection vulnerable code. Where do you check whether the $start variable really contains a number, not a dangerous SQL code? Nowhere. Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939168 Share on other sites More sharing options...
Noskiw Posted October 18, 2009 Author Share Posted October 18, 2009 that didn't really help... Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939179 Share on other sites More sharing options...
JAY6390 Posted October 18, 2009 Share Posted October 18, 2009 This is exactly why I created a php pagination class. It's really simple to implement and doesn't require lines and lines of code to get it working. If you go to my link, then save the code to pagination.php in the same folder as your script above is in, then use this code <?php $title = "Guestbook"; $connect = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("guest") or die(mysql_error()); function bbcode($string) { if ($string) { $bbcode_array = array('[b]', '[/b]', '[u]', '[/u]', '[i]', '[/i]', '[code]', ' ', ''); $bbcode_array_2 = array('<b>', '</b>', '<u>', '</u>', '<i>', '</i>', '<center><div style="width:90%;padding:3px;background-color:#000099;color:#FFFFFF;border:2px solid;">', '</div></center>', '<img src="', '">'); $new_string_2 = str_ireplace($bbcode_array, $bbcode_array_2, $string); return $new_string_2; } } echo "<h1>Guestbook</h1><hr />"; //start pagination $query = "SELECT * FROM guest ORDER BY id DESC"; include('pagination.php'); $paginator = new pagination($_GET['start'], $query); $paginator->results_per_page = 5; $paginator->padding(3); $paginator->link_prefix = '/index.php?p=guestbook&start='; $paginator->link_suffix = ''; $paginator->page_nums_separator = ' | '; while ($row2 = mysql_fetch_assoc($get)) { $name2 = $row2['name']; $email2 = $row2['email']; $message2 = $row2['message']; $date2 = $row2['date']; $time2 = $row2['time']; echo "<table><tr><td><b>Posted by: ".$name2."(".$email2.") on ".$date2." at ".$time2."</b></td></tr><tr><td>".nl2br(bbcode(strip_tags($message2))). "</td></tr></table>"; echo "<hr />"; } echo $paginator; if ($_POST['submit']) { $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $date = date("Y-m-d"); $time = date("H:i:s"); if ($name && $email && $message) { $querypost = mysql_query("INSERT INTO guest VALUES('','".$name."','".$email."','".$message."','".$date."','".$time."')"); echo "Please wait... <meta http-equiv='refresh' content='2'>"; } else { echo "Please fill out all fields!"; } } echo "<hr />"; echo " <form action='guestbook.php' method='POST'> <table width='100%'> <tr> <td width='7%' valign='top'> Your Name: </td> <td valign='top'> <input type='text' name='name' maxlength='25' /> </td> </tr> <tr> <td valign='top'> Your Email: </td> <td> <input type='text' name='email' maxlength='25' /> </td> </tr> <tr> <td valign='top'> Your Message: </td> <td> <textarea cols='20' rows='2' name='message' maxlength='250'></textarea> <p><input type='submit' name='submit' value='Post' /> </td> </tr> </table> </form>"; ?>[/code] it should work nicely Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939190 Share on other sites More sharing options...
Noskiw Posted October 18, 2009 Author Share Posted October 18, 2009 it doesn't work... Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in C:\xampp\htdocs\dynamic web page\inc\guestbook.php on line 70 Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939196 Share on other sites More sharing options...
JAY6390 Posted October 18, 2009 Share Posted October 18, 2009 Oops change while ($row2 = mysql_fetch_assoc($get)) { to while ($row2 = mysql_fetch_assoc($paginator->resource())) { Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939197 Share on other sites More sharing options...
Noskiw Posted October 18, 2009 Author Share Posted October 18, 2009 i still have the same problem... Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939200 Share on other sites More sharing options...
JAY6390 Posted October 18, 2009 Share Posted October 18, 2009 <?php $title = "Guestbook"; $connect = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("guest") or die(mysql_error()); function bbcode($string) { if ($string) { $bbcode_array = array('[b]', '[/b]', '[u]', '[/u]', '[i]', '[/i]', '[code]', ' ', ''); $bbcode_array_2 = array('<b>', '</b>', '<u>', '</u>', '<i>', '</i>', '<center><div style="width:90%;padding:3px;background-color:#000099;color:#FFFFFF;border:2px solid;">', '</div></center>', '<img src="', '">'); $new_string_2 = str_ireplace($bbcode_array, $bbcode_array_2, $string); return $new_string_2; } } echo "<h1>Guestbook</h1><hr />"; //start pagination $query = "SELECT * FROM guest ORDER BY id DESC"; include('pagination.php'); $paginator = new pagination($_GET['start'], $query); $paginator->results_per_page = 5; $paginator->padding(3); $paginator->link_prefix = '/index.php?p=guestbook&start='; $paginator->link_suffix = ''; $paginator->page_nums_separator = ' | '; $paginator->paginate(); while ($row2 = mysql_fetch_assoc($paginator->resource())) { $name2 = $row2['name']; $email2 = $row2['email']; $message2 = $row2['message']; $date2 = $row2['date']; $time2 = $row2['time']; echo "<table><tr><td><b>Posted by: ".$name2."(".$email2.") on ".$date2." at ".$time2."</b></td></tr><tr><td>".nl2br(bbcode(strip_tags($message2))). "</td></tr></table>"; echo "<hr />"; } echo $paginator; if ($_POST['submit']) { $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $date = date("Y-m-d"); $time = date("H:i:s"); if ($name && $email && $message) { $querypost = mysql_query("INSERT INTO guest VALUES('','".$name."','".$email."','".$message."','".$date."','".$time."')"); echo "Please wait... <meta http-equiv='refresh' content='2'>"; } else { echo "Please fill out all fields!"; } } echo "<hr />"; echo " <form action='guestbook.php' method='POST'> <table width='100%'> <tr> <td width='7%' valign='top'> Your Name: </td> <td valign='top'> <input type='text' name='name' maxlength='25' /> </td> </tr> <tr> <td valign='top'> Your Email: </td> <td> <input type='text' name='email' maxlength='25' /> </td> </tr> <tr> <td valign='top'> Your Message: </td> <td> <textarea cols='20' rows='2' name='message' maxlength='250'></textarea> <p><input type='submit' name='submit' value='Post' /> </td> </tr> </table> </form>"; ?>[/code]Try that Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939204 Share on other sites More sharing options...
Noskiw Posted October 18, 2009 Author Share Posted October 18, 2009 that gives me a list of my folders... Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939206 Share on other sites More sharing options...
JAY6390 Posted October 18, 2009 Share Posted October 18, 2009 It should give you the first 5 results from the guest table. Can you give more info/a screenshot Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939207 Share on other sites More sharing options...
Noskiw Posted October 18, 2009 Author Share Posted October 18, 2009 [/img] [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939209 Share on other sites More sharing options...
JAY6390 Posted October 18, 2009 Share Posted October 18, 2009 change $paginator->link_prefix = '/index.php?p=guestbook&start='; to $paginator->link_prefix = 'index.php?p=guestbook&start='; (I assumed this wasn't in a subfolder) You should always use underscores instead of spaces for folders in web directories too Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939212 Share on other sites More sharing options...
Noskiw Posted October 18, 2009 Author Share Posted October 18, 2009 i have the same problem... Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939215 Share on other sites More sharing options...
Noskiw Posted October 18, 2009 Author Share Posted October 18, 2009 i have the same problem again...... Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939242 Share on other sites More sharing options...
JAY6390 Posted October 18, 2009 Share Posted October 18, 2009 $paginator->link_prefix = '/dynamic web page/index.php?p=guestbook&start='; Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939281 Share on other sites More sharing options...
Noskiw Posted October 18, 2009 Author Share Posted October 18, 2009 honestly, this stuff doesn't work for me... Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939298 Share on other sites More sharing options...
JAY6390 Posted October 18, 2009 Share Posted October 18, 2009 The pagination is working properly. The only thing I can guess from it is that your folder naming is causing some kind of issue. If you set the link prefix to the exact URL of the pagination and see if that works If you type in http://localhost/dynamic web page/index.php?p=guestbook&start=1 http://localhost/dynamic web page/index.php?p=guestbook&start=2 http://localhost/dynamic web page/index.php?p=guestbook&start=3 into your browser do they work? (assuming there is a third page) Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939352 Share on other sites More sharing options...
Noskiw Posted October 19, 2009 Author Share Posted October 19, 2009 thankyou, that worked.... xD Quote Link to comment https://forums.phpfreaks.com/topic/178118-pagination-problem/#findComment-939889 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.