graham23s Posted December 30, 2007 Share Posted December 30, 2007 Hi Guys, i decided to write a pagination script, after reading up quite a bit on them (i wanted to master it fairly quickly as its always been a thorn in my side lol) this code works kind of, it displays the page number links only after i click nect, when i click previous it makes a link disappear code: <?php // database connection // include("db_connection.php"); // pagination script start // // number of records to display per page // $display_per_page = 10; // check the required number of pages // if(isset($_GET['page'])) { $number_of_pages = $_GET['page']; } else { // query for the number of pages // $query = "SELECT COUNT(*) FROM `files`"; $result = mysql_query($query); // put the number of results in an array // $row = mysql_fetch_array($result); // this contains the overall TOTAL number of results in the db // $total_number_of_results = $row['0']; // calculate number of pages // if($total_number_of_results > $display) { $number_of_pages = ceil($total_number_of_results/$display_per_page); } else { $number_of_pages = 1; } } // end of number of pages if // // now determine the start of the results // if(isset($_GET['page'])) { $start_page = $_GET['page']; } else { $start_page = 0; } // do another query this time use the variables in the limit clause // $query = "SELECT * FROM `files` LIMIT $start_page, $display_per_page"; $result = mysql_query($query); // loop out the results // while($row = mysql_fetch_array($result)) { $id = $row['id']; echo ("$id"); echo ("<br />"); } echo ("<hr />"); // 10 results displayed great // // now make the links // if($number_of_pages > 1) { // find out what exactly the page the user is on [start page say 2 x 10 +1] // $current_page_the_user_is_on = ($start_page/$display_per_page) + 1; // if its not the first page we need a previous button // if($current_page_the_user_is_on != 1) // echo a previous button // // vars // $prev = ($number_of_pages - 1); $next = ($number_of_pages + 1); echo ("<a href=\"index.php?page=$prev\">PREV</a> "); } // make all the numbered pages // for($i = 1; $i <= $number_of_pages; $i++) { echo ("<a href=\"index.php?page=$i\">$i</a> "); } // finally make the next link // if($current_page_the_user_is_on != $number_of_pages) { echo ("<a href=\"index.php?page=$next\">NEXT</a> "); } ?> is there any improvements i could make or things i haven't done, any input would be appreciated cheers Graham Quote Link to comment https://forums.phpfreaks.com/topic/83770-first-pagination-script/ Share on other sites More sharing options...
cooldude832 Posted December 30, 2007 Share Posted December 30, 2007 1) No Error checkin on mysql quereies 2) No validation of get values for integer use the is_int insted of isset 3) * select on the primary query is over kill probably Quote Link to comment https://forums.phpfreaks.com/topic/83770-first-pagination-script/#findComment-426209 Share on other sites More sharing options...
graham23s Posted December 31, 2007 Author Share Posted December 31, 2007 Hi Mate, cool, so basically scrap the "isset", i asn't to fussed on making any error checking as i was just trying to get the basics down, thanks for the info mate:) Graham Quote Link to comment https://forums.phpfreaks.com/topic/83770-first-pagination-script/#findComment-426668 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.