ChompGator Posted January 23, 2009 Share Posted January 23, 2009 Hello, I have a real simple question, I have a php script that displays news articles on a page...Im curious as to what would I add to this script to tell it after every 4 articles, add a new page - and Id just want it to start creating page numbers like: Page: [1], [2], [3] <?php $con = mysql_connect("***","***","****") or die('Could not connect: ' . mysql_error()); mysql_select_db("***", $con); $result = mysql_query("SELECT * FROM elections"); while($row = mysql_fetch_assoc($result)){ echo "ID: ".$row['id']." - ".$row['articlename']." -- ".$row['date']."<br/><br/> ".$row['description']."<br/><br/>"; } ?>< Any help is appreciated - thanks! Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/ Share on other sites More sharing options...
rhodesa Posted January 23, 2009 Share Posted January 23, 2009 http://www.phpfreaks.com/tutorial/basic-pagination Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744602 Share on other sites More sharing options...
jscix Posted January 23, 2009 Share Posted January 23, 2009 Count the total number of rows. Divide that number by 4. You have how many pages you need. EG: [1] - [2] ETC. Add a 'Limit 4' To each query. Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744603 Share on other sites More sharing options...
ChompGator Posted January 23, 2009 Author Share Posted January 23, 2009 http://www.phpfreaks.com/tutorial/basic-pagination Hey there, Ok tried out this tutorial, customized it to my db...and applied it to my page, but its not displaying any news articles at all. Im not getting any errors from the script, my page is just coming up blank.. Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744619 Share on other sites More sharing options...
Maq Posted January 23, 2009 Share Posted January 23, 2009 http://www.phpfreaks.com/tutorial/basic-pagination Hey there, Ok tried out this tutorial, customized it to my db...and applied it to my page, but its not displaying any news articles at all. Im not getting any errors from the script, my page is just coming up blank.. You did that tutorial in 15 minutes, man that's quick. Do you have error reporting turned on? Do you echo out variables and sql errors? Can we see your code? Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744627 Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 MAke sure your errors are turne3d on: Put this as the VERY first thing that gets executed. ini_set('display_errors',1); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744628 Share on other sites More sharing options...
ChompGator Posted January 23, 2009 Author Share Posted January 23, 2009 Hey, Yeah I quickly read through the tutorial to get an idea of what it is that needs to be done...Then I applied it to my code..I believe error reporting is turned...But here is the code, if you want to check it out <?php $con = mysql_connect("","","") or die('Could not connect: ' . mysql_error()); mysql_select_db("legion", $con); $result = mysql_query("SELECT * FROM elections"); while($row = mysql_fetch_assoc($result)){ echo "ID: ".$row['id']." - ".$row['articlename']." -- ".$row['date']."<br/><br/> ".$row['description']."<br/><br/>"; } // end if // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM elections"; $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; // get the info from the db $sql = "SELECT id, number FROM elections LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { // echo data echo $list['id'] . " : " . $list['number'] . "<br />"; } // end while /****** 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744653 Share on other sites More sharing options...
Maq Posted January 23, 2009 Share Posted January 23, 2009 Like uniflare and I had already mentioned, at the top of your script put: ini_set ("display_errors", "1"); error_reporting(E_ALL); The only thing that you have on is mysql errors. Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744658 Share on other sites More sharing options...
ChompGator Posted January 23, 2009 Author Share Posted January 23, 2009 Ok, done, Now its displaying the news articles, but its not creating the pagination, here are the errors its returning: The weird thing is this script is only 100 lines, it doesn't go to 208 Notice: Undefined variable: conn in D:\hosting\member\264legion\site1\elections\index.php on line 208 Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\hosting\member\264legion\site1\elections\index.php on line 208 Fatal error: SQL in D:\hosting\member\264legion\site1\elections\index.php on line 208 PHP Notice: Undefined variable: conn in D:\hosting\member\264legion\site1\elections\index.php on line 208 PHP Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\hosting\member\264legion\site1\elections\index.php on line 208 PHP Fatal error: SQL in D:\hosting\member\264legion\site1\elections\index.php on line 208 Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744671 Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 ou onl,y need to use mysql_query($sql); since mysql_query will use the current open conection. you ca use if(isset($variable)){ /* do somethign with this variable */ } to rid yourself of those "PHP Notice: Undefined variable" errors. Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744672 Share on other sites More sharing options...
ChompGator Posted January 23, 2009 Author Share Posted January 23, 2009 ou onl,y need to use mysql_query($sql); since mysql_query will use the current open conection. you ca use if(isset($variable)){ /* do somethign with this variable */ } to rid yourself of those "PHP Notice: Undefined variable" errors. I fixed that portion, the error is still appearing though Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744678 Share on other sites More sharing options...
ChompGator Posted January 23, 2009 Author Share Posted January 23, 2009 Ok, the errors are gone, the only error left is Fatal error: SQL in D:\hosting\member\264legion\site1\elections\index.php on line 242 PHP Fatal error: SQL in D:\hosting\member\264legion\site1\elections\index.php on line 242 Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744681 Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); use mysql_query($sql) or die(mysql_error()); instead Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744683 Share on other sites More sharing options...
Maq Posted January 23, 2009 Share Posted January 23, 2009 How about show us line 242 and some of the surrounding code? Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744686 Share on other sites More sharing options...
ChompGator Posted January 23, 2009 Author Share Posted January 23, 2009 Here is the new code, double-check it to make sure I did what you said properly... I made that replacement, but Im still getting the Fatal Error: <?php $con = mysql_connect("***","***","***") or die('Could not connect: ' . mysql_error()); mysql_select_db("legion", $con); $result = mysql_query("SELECT * FROM elections"); while($row = mysql_fetch_assoc($result)){ echo "ID: ".$row['id']." - ".$row['articlename']." -- ".$row['date']."<br/><br/> ".$row['description']."<br/><br/>"; } // end if ini_set ("display_errors", "1"); error_reporting(E_ALL); // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM elections"; $result = mysql_query($sql) or die(mysql_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; // get the info from the db $sql = "SELECT id, number FROM elections LIMIT $offset, $rowsperpage"; $result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR); // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { // echo data echo $list['id'] . " : " . $list['number'] . "<br />"; } // end while /****** 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 ******/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744687 Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 please try the suggestion Instead of: mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); use mysql_query($sql) or die(mysql_error()); instead Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744691 Share on other sites More sharing options...
.josh Posted January 23, 2009 Share Posted January 23, 2009 your script is only 100 lines long but it's saying the error is on line 242... if that was the complete code you posted, is this file being included in some other file? If not, then perhaps you're trying to run the wrong script. Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744709 Share on other sites More sharing options...
ChompGator Posted January 23, 2009 Author Share Posted January 23, 2009 Ok, I got it all fixed... The problem is now, its showing the page numbers ie: [1], [2] etc.. But its showing them like this: 3:3 4:4 5:5 And its still displaying all the news articles on one page, not 4 on each page. Thanks everyone fior all the input, any more help would be great Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744733 Share on other sites More sharing options...
ChompGator Posted January 23, 2009 Author Share Posted January 23, 2009 your script is only 100 lines long but it's saying the error is on line 242... if that was the complete code you posted, is this file being included in some other file? If not, then perhaps you're trying to run the wrong script. Hey, thanks for your input so far, the script is only infact 100 lines long... So why its saying there is an error on line 242, is beyond me. Ive since gotten all the page numbers to appear and all the errors to disappear, the only thing Im fighting with now, is its still showing all the news articles in the database on one page, I need it to only show 4 per page, and right now its showing all the articles in the database on one page, then when you click to page two, it shows all the articles again, and so on.. But the page numbers are showing and they are working so thats a step forward Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744756 Share on other sites More sharing options...
Maq Posted January 23, 2009 Share Posted January 23, 2009 thanks for your input so far, the script is only infact 100 lines long... So why its saying there is an error on line 242, is beyond me. Well is this file, "site1\elections\index.php"? Is it an include? Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744761 Share on other sites More sharing options...
ChompGator Posted January 23, 2009 Author Share Posted January 23, 2009 Nope, the script is running on the index.php it shouldn't be an include, Ill find it and remove it Heres the most updated code if you want to take a look <?php $con = mysql_connect("","","") or die('Could not connect: ' . mysql_error()); mysql_select_db("", $con); $result = mysql_query("SELECT * FROM elections"); while($row = mysql_fetch_assoc($result)){ echo "ID: ".$row['id']." - ".$row['articlename']." -- ".$row['date']."<br/><br/> ".$row['description']."<br/><br/>"; } // end if ini_set ("display_errors", "1"); error_reporting(E_ALL); // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM elections"; $result = mysql_query($sql) or die(mysql_error()); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 1; // 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; // get the info from the db $sql = "SELECT id, number FROM elections LIMIT $offset, $rowsperpage"; $result = mysql_query($sql) or die(mysql_error()); // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { // echo data echo $list['id'] . " : " . $list['number'] . "<br />"; } // end while /****** 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 ******/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744767 Share on other sites More sharing options...
uniflare Posted January 24, 2009 Share Posted January 24, 2009 Echo the variables make sure the are what you want them to be. i'd start with the query (would be why your getting every row rather than the 4 you want): echo("SQL QUERY: ".$sql."<br />"); $sql = "SELECT id, number FROM elections LIMIT $offset, $rowsperpage"; $result = mysql_query($sql) or die(mysql_error()); Then with the variables that deal with the page numbers. ----- Also when you add code, highlight it and click the hash button ( # ), just above the emoticons (smileys). Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-744905 Share on other sites More sharing options...
ChompGator Posted January 25, 2009 Author Share Posted January 25, 2009 Hey, Ok, well that makes sense, echo the query...but after thats done how would I alter the query - so that it only displays four news-article entries per-page. This is a first for doing pagination for me, so I appreciate all the help thanks! Quote Link to comment https://forums.phpfreaks.com/topic/142147-indexing-pages/#findComment-745878 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.