confused_aswell Posted October 15, 2008 Share Posted October 15, 2008 Hi I am trying all different kinds of pagination scripts from the internet, but I can't get any of them to work. Can someone help me through this please? Thanks, Phil Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/ Share on other sites More sharing options...
Lamez Posted October 15, 2008 Share Posted October 15, 2008 <?php $num = $over_num; $page = isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] >= 1 ? $_GET['page'] : 1; $per_page = $num; $count_q = mysql_query("SELECT COUNT(id) FROM home ORDER BY id DESC"); $count_r = mysql_fetch_row($count_q); $count = $count_r[0]; if($page > ceil($count/$per_page)) $page = 1; $limit_min = ($page-1)*$per_page; $query = mysql_query("SELECT * FROM home ORDER BY id DESC LIMIT {$limit_min}, {$per_page}"); $sql = mysql_query("SELECT * FROM home ORDER BY id DESC"); $num_rows = mysql_num_rows($sql); $limit2 = $per_page; $q = "SELECT id FROM home"; $r = mysql_query($q); $num = ceil(mysql_num_rows($r) / $limit2); if($num != 1) { for($i = 1; $i <= $num; $i++) { /*if($page / $limit2 != $i)*/ echo "[<a href='?p=list&page=".$i."'>Page ".$i."</a>] "; //else echo "[<b>".$i."</b>] "; } } echo "<br /><br />"; //while($row = mysql_fetch_array($sql)){ while($row = mysql_fetch_assoc($query)) { //echo out DB info here } $limit2 = $per_page; $q = "SELECT id FROM home"; $r = mysql_query($q); $num = ceil(mysql_num_rows($r) / $limit2); if($num != 1) { for($i = 1; $i <= $num; $i++) { /*if($page / $limit2 != $i)*/ echo "[<a href='?p=list&page=".$i."'>Page ".$i."</a>] "; //else echo "[<b>".$i."</b>] "; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/#findComment-666430 Share on other sites More sharing options...
confused_aswell Posted October 15, 2008 Author Share Posted October 15, 2008 Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/phcleani/public_html/display_records.php on line 11 Warning: Division by zero in /home/phcleani/public_html/display_records.php on line 14 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/phcleani/public_html/display_records.php on line 21 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/phcleani/public_html/display_records.php on line 27 Warning: Division by zero in /home/phcleani/public_html/display_records.php on line 27 Hi I have changed the sql queries to orders_id (id)and zen_orders (table) but I got these errors. Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/phcleani/public_html/display_records.php on line 38 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/phcleani/public_html/display_records.php on line 48 Warning: Division by zero in /home/phcleani/public_html/display_records.php on line 48 Report to moderator Logged Thanks Phil <?php require_once('Connections/zen.php'); ?> <?php $num = $over_num; $page = isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] >= 1 ? $_GET['page'] : 1; $per_page = $num; $count_q = mysql_query("SELECT COUNT(orders_id) FROM zen_orders ORDER BY orders_id DESC"); $count_r = mysql_fetch_row($count_q); $count = $count_r[0]; if($page > ceil($count/$per_page)) $page = 1; $limit_min = ($page-1)*$per_page; $query = mysql_query("SELECT * FROM zen_orders ORDER BY id DESC LIMIT {$limit_min}, {$per_page}"); $sql = mysql_query("SELECT * FROM zen_orders ORDER BY id DESC"); $num_rows = mysql_num_rows($sql); $limit2 = $per_page; $q = "SELECT orders_id FROM zen_orders"; $r = mysql_query($q); $num = ceil(mysql_num_rows($r) / $limit2); if($num != 1) { for($i = 1; $i <= $num; $i++) { /*if($page / $limit2 != $i)*/ echo "[<a href='?p=list&page=".$i."'>Page ".$i."</a>] "; //else echo "[<b>".$i."</b>] "; } } echo "<br /><br />"; //while($row = mysql_fetch_array($sql)){ while($row = mysql_fetch_assoc($query)) { //echo out DB info here } $limit2 = $per_page; $q = "SELECT orders_id FROM zen_orders"; $r = mysql_query($q); $num = ceil(mysql_num_rows($r) / $limit2); if($num != 1) { for($i = 1; $i <= $num; $i++) { /*if($page / $limit2 != $i)*/ echo "[<a href='?p=list&page=".$i."'>Page ".$i."</a>] "; //else echo "[<b>".$i."</b>] "; } } ?> Report to moderator Logged Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/#findComment-666438 Share on other sites More sharing options...
dropfaith Posted October 15, 2008 Share Posted October 15, 2008 http://www.phpfreaks.com/tutorial/basic-pagination the best tutorial i have found on this Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/#findComment-666542 Share on other sites More sharing options...
Lamez Posted October 15, 2008 Share Posted October 15, 2008 it is because the database has nothing in it. Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/#findComment-666568 Share on other sites More sharing options...
confused_aswell Posted October 16, 2008 Author Share Posted October 16, 2008 Hi I tried this one too, but I couldn't get it to work either! Thanks, Phil Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/#findComment-666835 Share on other sites More sharing options...
dropfaith Posted October 16, 2008 Share Posted October 16, 2008 it seems your database has no results from those errors Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/#findComment-666859 Share on other sites More sharing options...
confused_aswell Posted October 16, 2008 Author Share Posted October 16, 2008 Hi I can connect to my db from Dreamweaver and I can show the results, but I don't want to use Dreamweaver as this has to be a stand alone script. Thanks, Phil Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/#findComment-666941 Share on other sites More sharing options...
Lamez Posted October 16, 2008 Share Posted October 16, 2008 make a new page and call out the info in the database, that you are trying to call here, just in case Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/#findComment-666954 Share on other sites More sharing options...
confused_aswell Posted October 16, 2008 Author Share Posted October 16, 2008 Here is the page that will call the info from the database. Thanks, Phil <?php require_once('Connections/zen.php'); ?> <?php mysql_select_db($database_zen, $zen); $query_Recordset1 = "SELECT * FROM zen_orders"; $Recordset1 = mysql_query($query_Recordset1, $zen) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); ?> <?php mysql_free_result($Recordset1); ?> Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/#findComment-666976 Share on other sites More sharing options...
confused_aswell Posted October 16, 2008 Author Share Posted October 16, 2008 Here is the PHPFreaks pagination tutorial that I have tried to upload This is the error I am getting - Fatal error: SQL in /home/phcleani/public_html/display_records4.php on line 42 Line 42 is - $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); <?php // database connection info $conn = mysql_connect('localhost','dbusername','dbpass') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('dbname',$conn) or trigger_error("SQL", E_USER_ERROR); // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM numbers"; $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 numbers 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) - 1); $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/128592-solved-a-simple-pagination-script/#findComment-666989 Share on other sites More sharing options...
dropfaith Posted October 16, 2008 Share Posted October 16, 2008 your not connected to your db Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/#findComment-667536 Share on other sites More sharing options...
GingerRobot Posted October 16, 2008 Share Posted October 16, 2008 I would suggest changing this: or trigger_error("SQL", E_USER_ERROR); To this: or trigger_error(mysql_error(), E_USER_ERROR); To gain slightly more informative information on the error. Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/#findComment-667544 Share on other sites More sharing options...
confused_aswell Posted October 16, 2008 Author Share Posted October 16, 2008 Hi I thought I'd take another look at the sql query and guess what, I found an error - durh! It's always the small things that get you. Thanks for your reply and your help of course. I will need some help in making the alternate rows background a different color, I don't know if you can help with that, butI am just glad it's working. All the best, Phil PS I will mark this thread solved. Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/#findComment-667555 Share on other sites More sharing options...
.josh Posted October 17, 2008 Share Posted October 17, 2008 Just throw a ternary assignment into the loop. Something like this: $color = ($color == '#FFFFFF')? '#000000' : '#FFFFFF'; Then just throw the $color into the bgcolor value or however you have the html/css setup. Color will initially be assigned white because it will initially not be anything. Next loop iteration since it's white, it will be assigned black. Just keeps switching back and forth like a uh, switch. You can, of course, change what colors you want to use. Quote Link to comment https://forums.phpfreaks.com/topic/128592-solved-a-simple-pagination-script/#findComment-667594 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.