netpumber Posted December 20, 2009 Share Posted December 20, 2009 Halo my friends! Im little new in php coding and so i need your useful help I have this php code that prints data from mysql.. <?php //[sTART] Retrieving DATA from MySql ini_set ('display_errors',1); error_reporting (E_ALL & ~E_NOTICE); if ($dbc = @mysql_connect ('localhost','user','pass')){ if (!@mysql_select_db ('web_site')){ die('<p>Could not select the database because:<b>'. mysql_error .'</b></p>'); } }else{ die('<p>Could not connect to MYSQL because:<b>' . mysql_error() . '</b></p>'); } $query = "SELECT * FROM site_entries ORDER BY date_entered DESC"; if ($r = mysql_query($query)){ while ($row = mysql_fetch_array($r)){ print "<tr> <td valign=\"top\"> <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"15\" bgcolor=\"#f2f2f2\"> <tr><td> <h3>{$row['title']}</h3><br> {$row['entry']} </td></tr> </table> </td> </tr>"; } }else{ .....blah blah blah.... So.. i want this script print in the first page only 3 entries and the others in other pages.. Have you any idea on how to achieve this ? Thanks in advanced! Quote Link to comment https://forums.phpfreaks.com/topic/185762-multiple-paging-help/ Share on other sites More sharing options...
rajivgonsalves Posted December 20, 2009 Share Posted December 20, 2009 you should try finding tutorials on pagination, I modified the code following it is but have not tested it out <?php //[sTART] Retrieving DATA from MySql $perpage = 3; ini_set ('display_errors',1); error_reporting (E_ALL & ~E_NOTICE); if ($dbc = @mysql_connect ('localhost','user','pass')){ if (!@mysql_select_db ('web_site')){ die('<p>Could not select the database because:<b>'. mysql_error .'</b></p>'); } }else{ die('<p>Could not connect to MYSQL because:<b>' . mysql_error() . '</b></p>'); } $page = isset($_GET['page']) ? $_GET['page'] : 1; $start = ($page - 1) * $perpage; $query = "SELECT * FROM site_entries ORDER BY date_entered DESC limit $start, $perpage"; if ($r = mysql_query($query)){ $total = mysql_num_rows($r); $pages = range(1, floor($total/$perpage)); $pagination = array(); foreach ($pages as $pageno) { if ($pageno == $page) { array_push($pagination, $page); } else { array_push($pagination, "<a href=\"".$_SERVER['PHP_SELF']."?page=$pagno\">$pageno</a>"); } } echo "<tr><td>".implode(" ", $pagination)."</td></tr>"; while ($row = mysql_fetch_array($r)){ print "<tr> <td valign=\"top\"> <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"15\" bgcolor=\"#f2f2f2\"> <tr><td> <h3>{$row['title']}</h3><br> {$row['entry']} </td></tr> </table> </td> </tr>"; } }else{ } Quote Link to comment https://forums.phpfreaks.com/topic/185762-multiple-paging-help/#findComment-980895 Share on other sites More sharing options...
netpumber Posted December 20, 2009 Author Share Posted December 20, 2009 Yes i found some tutorials but i want also and community's opinion.. Thanks anyway i ll try it out.. Quote Link to comment https://forums.phpfreaks.com/topic/185762-multiple-paging-help/#findComment-980896 Share on other sites More sharing options...
netpumber Posted December 20, 2009 Author Share Posted December 20, 2009 hmm that so so works... In main page it prints the first 3 entries but it doesnt have links for the second and third page. If i add this in url ?page= with 2 or 3 in variable page it sows me the next entries..and then appeared links 1 and 0 and when i visit them returns me this error : Could not retrive the data brcause:mysql_error The query was $query. Anyway.. this is a start... Quote Link to comment https://forums.phpfreaks.com/topic/185762-multiple-paging-help/#findComment-980910 Share on other sites More sharing options...
netpumber Posted December 20, 2009 Author Share Posted December 20, 2009 You had an error here : array_push($pagination, "<a href=\"".$_SERVER['PHP_SELF']."?page=$pagno\">$pageno</a>"); with variable pagno and pageno They are different but it must be the same so now it works little better. The prob is that in the first page doesnt prints the numbers of the other pages.. And in the third page as i said before it prints links with pages 1 and 0 . when you visit 1 it works when you visit 0 it returns the above error.. Actually i think that 0 occurs because of this : $start = ($page - 1) * $perpage; Actually here we multiply (1 -1) * 3 = 0 * 3 = 0 This is my opinion. So something must be change here Mabe the $page = isset($_GET['page']) ? $_GET['page'] : 1; So the $page not to start from 1 .. I dont know Quote Link to comment https://forums.phpfreaks.com/topic/185762-multiple-paging-help/#findComment-980912 Share on other sites More sharing options...
rajivgonsalves Posted December 20, 2009 Share Posted December 20, 2009 sorry about that my mistake as I said the code was untested however this should work <?php //[sTART] Retrieving DATA from MySql $perpage = 3; $page = isset($_GET['page']) ? $_GET['page'] : 1; $start = ($page - 1) * $perpage; ini_set ('display_errors',1); error_reporting (E_ALL & ~E_NOTICE); if ($dbc = @mysql_connect ('localhost','user','pass')){ if (!@mysql_select_db ('web_site')){ die('<p>Could not select the database because:<b>'. mysql_error .'</b></p>'); } }else{ die('<p>Could not connect to MYSQL because:<b>' . mysql_error() . '</b></p>'); } $query = "SELECT * FROM site_entries ORDER BY date_entered DESC limit $start, $perpage"; $count_query = "select count(*) as count FROM site_entries"; $count_result = mysql_query($count_query); $count_array = mysql_fetch_array($count_result); $total = $count_array['count']; if ($r = mysql_query($query)){ $pages = range(1, floor($total/$perpage)); $pagination = array(); foreach ($pages as $pageno) { if ($pageno == $page) { array_push($pagination, $page); } else { array_push($pagination, "<a href=\"".$_SERVER['PHP_SELF']."?page=$pageno\">$pageno</a>"); } } echo "<tr><td>".implode(" ", $pagination)."</td></tr>"; while ($row = mysql_fetch_array($r)){ print "<tr> <td valign=\"top\"> <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"15\" bgcolor=\"#f2f2f2\"> <tr><td> <h3>{$row['title']}</h3><br> {$row['entry']} </td></tr> </table> </td> </tr>"; } }else{ } Quote Link to comment https://forums.phpfreaks.com/topic/185762-multiple-paging-help/#findComment-980929 Share on other sites More sharing options...
keldorn Posted December 20, 2009 Share Posted December 20, 2009 Unless your into reinventing wheels that end up square, use this class http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/ Quote Link to comment https://forums.phpfreaks.com/topic/185762-multiple-paging-help/#findComment-980944 Share on other sites More sharing options...
netpumber Posted December 20, 2009 Author Share Posted December 20, 2009 hmm i follow finally this tutorial.. and it works Great!! Thanks all of you !! http://www.phpfreaks.com/tutorial/basic-pagination Quote Link to comment https://forums.phpfreaks.com/topic/185762-multiple-paging-help/#findComment-980947 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.