runnerjp Posted October 29, 2009 Share Posted October 29, 2009 <?php //This code will obtain the required page number from the $_GET array. Note that if it is not present it will default to 1. if (isset($_GET['pageno'])) { $pageno = $_GET['pageno']; } else { $pageno = 1; } // if //This code will count how many rows will satisfy the current query. $query ="SELECT * FROM `photos` WHERE `thumb_id` = '3'"; $result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR); $query_data = mysql_fetch_row($result); $numrows = $query_data[0]; //This code uses the values in $rows_per_page and $numrows in order to identify the number of the last page. $rows_per_page = 3; $lastpage = ceil($numrows/$rows_per_page); //This code checks that the value of $pageno is an integer between 1 and $lastpage. $pageno = (int)$pageno; if ($pageno > $lastpage) { $pageno = $lastpage; } // if if ($pageno < 1) { $pageno = 1; } // if //This code will construct the LIMIT clause for the sql SELECT statement. $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page; //Now we can issue the database qery and process the result. $sql="SELECT * FROM `photos` WHERE `thumb_id` = '3'"; $res = mysql_query($sql) or die(mysql_error()); echo "<table>"; $i = 0; while($row = mysql_fetch_assoc($res)){ $i++; if ($i % 3 == 0) echo '<tr>'; echo "<td> <img src=\"http://www.runningprofiles.com/members/include/myimages/thumbs/" . $row['picurl'] . "\" /></a><br /> </td>"; if ($i % 3 == 0) echo '</tr>'; if( $i == 9 ) break; } echo "</table>"; //... process contents of $result ... //Finally we must construct the hyperlinks which will allow the user to select other pages. We will start with the links for any previous pages. if ($pageno == 1) { echo " FIRST PREV "; } else { echo " <a href='http://www.runningprofiles.com/members/index.php?page=album&pageno=1'>FIRST</a> "; $prevpage = $pageno-1; echo " <a href='http://www.runningprofiles.com/members/index.php?page=album&pageno=$prevpage'>PREV</a> "; } // if //Next we inform the user of his current position in the sequence of available pages. echo " ( Page $pageno of $lastpage ) "; //This code will provide the links for any following pages. if ($pageno == $lastpage) { echo " NEXT LAST "; } else { $nextpage = $pageno+1; echo " <a href='http://www.runningprofiles.com/members/index.php?page=album&pageno=$nextpage'>NEXT</a> "; echo " <a href='http://www.runningprofiles.com/members/index.php?page=album&pageno=$lastpage'>LAST</a> "; } // if ?> Im alittle confused by my pageinated results... i only have 2 images... and for some reason its repeated these images over 11 pages... how comes? Quote Link to comment https://forums.phpfreaks.com/topic/179504-pageination-not-working-right-coping-images-over-4-pages/ Share on other sites More sharing options...
lemmin Posted October 29, 2009 Share Posted October 29, 2009 Why do you do the same query twice? Try something like this: //Pagination variables if (!isset($_GET['page'])) $_GET['page'] = 0; $entriesPerPage = 5; $start = $_GET['page']*$entriesPerPage; //Querying $query = "SELECT *, (SELECT COUNT(*) FROM photos WHERE thumb_id = 3) as total FROM photos WHERE thumb_id = 3 LIMIT $start, $entriesPerPage"; $result = mysql_query($query) or die(mysql_error()); $total = mysql_result($result, 0, 'total'); //Do your displaying of images here //... //Pagination $string .= "<div><table width=100%><tr><td>"; if ($_GET['page']) $string .= "<a href=\"?page=0\"><<</a> <a href=\"?page=".($_GET['page']-1)."\">< Previous Page</a>"; $string .= "<td align=\"right\">"; if ($_GET['page']*$entriesPerPage+$entriesPerPage < $total) $string .= "<a href=\"?page=".($_GET['page']+1)."\">Next Page ></a> <a href=\"?page=".(floor(($total-1)/$entriesPerPage))."\">>></a>"; $string .= "</table></div>"; echo $string; Sorry if there are any errors in the code, I didn't test it. Quote Link to comment https://forums.phpfreaks.com/topic/179504-pageination-not-working-right-coping-images-over-4-pages/#findComment-947294 Share on other sites More sharing options...
runnerjp Posted October 29, 2009 Author Share Posted October 29, 2009 Ok so if im displaying it 3 row with 3 images in each and only 9 images...how would i stop it having previous ect if this isnt filled? <link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/css/login.css"> <?php require_once '../settings.php'; checkLogin('1 2'); include "../info.php"; // sets username/id ect include "../getuser.php"; // records user view on page //Pagination variables if (!isset($_GET['pagenum'])) $_GET['pagenum'] = 0; $entriesPerPage = 9; $start = $_GET['pagenum']*$entriesPerPage; //Querying $query = "SELECT *, (SELECT COUNT(*) FROM photos WHERE thumb_id = 3) as total FROM photos WHERE thumb_id = 3 LIMIT $start, $entriesPerPage"; $result = mysql_query($query) or die(mysql_error()); $total = mysql_result($result, 0, 'total'); $sql="SELECT * FROM `photos` WHERE `thumb_id` = '3'"; $res = mysql_query($sql) or die(mysql_error()); echo "<table>"; $i = 0; while($row = mysql_fetch_assoc($res)){ $i++; if ($i % 3 == 0) echo '<tr>'; echo "<td> <img src=\"http://www.runningprofiles.com/members/include/myimages/thumbs/" . $row['picurl'] . "\" /></a><br /> </td>"; if ($i % 3 == 0) echo '</tr>'; if( $i == 9 ) break; } echo "</table>"; $string .= "<div><table width=100%><tr><td>"; if ($_GET['page']) $string .= "<a href=\"?pagenum=0\"><<</a> <a href=\"?pagenum=".($_GET['pagenum']-1)."\">< Previous Page</a>"; $string .= "<td align=\"right\">"; if ($_GET['pagenum']*$entriesPerPage+$entriesPerPage < $total) $string .= "<a href=\"?pagenum=".($_GET['pagenum']+1)."\">Next Page ></a> <a href=\"?pagenum=".(floor(($total-1)/$entriesPerPage))."\">>></a>"; $string .= "</table></div>"; echo $string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/179504-pageination-not-working-right-coping-images-over-4-pages/#findComment-947366 Share on other sites More sharing options...
ashton321 Posted October 29, 2009 Share Posted October 29, 2009 Will this help you accomplish your task? Noth really sure what youre looking for. http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Quote Link to comment https://forums.phpfreaks.com/topic/179504-pageination-not-working-right-coping-images-over-4-pages/#findComment-947394 Share on other sites More sharing options...
mikesta707 Posted October 29, 2009 Share Posted October 29, 2009 im thinking here //Now we can issue the database qery and process the result. $sql="SELECT * FROM `photos` WHERE `thumb_id` = '3'"; $res = mysql_query($sql) or die(mysql_error()); you want to add the limit clause, to the end of the query. $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page; //Now we can issue the database qery and process the result. $sql="SELECT * FROM `photos` WHERE `thumb_id` = 3 " . $limit; $res = mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/179504-pageination-not-working-right-coping-images-over-4-pages/#findComment-947398 Share on other sites More sharing options...
runnerjp Posted October 30, 2009 Author Share Posted October 30, 2009 Ok i have it sorted but there is something i dont know how to do.... how can i display numbers like 12345... at the bottom dependant on how many pages there are? <?php require_once '../settings.php'; checkLogin('1 2'); include "../info.php"; // sets username/id ect include "../getuser.php"; // records user view on page //Pagination variables if (!isset($_GET['pagenum'])) $_GET['pagenum'] = 0; $entriesPerPage = 9; $start = $_GET['pagenum']*$entriesPerPage; //Querying $query = "SELECT *, (SELECT COUNT(*) FROM photos WHERE thumb_id = 3) as total FROM photos WHERE thumb_id = 3 LIMIT $start, $entriesPerPage"; $result = mysql_query($query) or die(mysql_error()); $total = mysql_result($result, 0, 'total'); $sql="SELECT * FROM `photos` WHERE `thumb_id` = '3'"; $res = mysql_query($sql) or die(mysql_error()); echo "<table>"; $i = 0; while($row = mysql_fetch_assoc($res)){ $i++; if ($i % 3 == 0) echo '<tr>'; echo "<td> <img src=\"http://www.runningprofiles.com/members/include/myimages/thumbs/" . $row['picurl'] . "\" /></a><br /> </td>"; if ($i % 3 == 0) echo '</tr>'; if( $i == 9 ) break; }echo "</table>"; if ($_GET['pagenum'] != '') { $string .= "<div><table width=100%><tr><td>"; if ($_GET['page']) $string .= "<a href=\"?pagenum=0\"><<</a> <a href=\"?pagenum=".($_GET['pagenum']-1)."\">< Previous Page</a>"; $string .= "<td align=\"right\">"; if ($_GET['pagenum']*$entriesPerPage+$entriesPerPage < $total) $string .= "<a href=\"?pagenum=".($_GET['pagenum']+1)."\">Next Page ></a> <a href=\"?pagenum=".(floor(($total-1)/$entriesPerPage))."\">>></a>"; $string .= "</table></div>"; echo $string; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/179504-pageination-not-working-right-coping-images-over-4-pages/#findComment-947878 Share on other sites More sharing options...
lemmin Posted October 30, 2009 Share Posted October 30, 2009 Ok so if im displaying it 3 row with 3 images in each and only 9 images...how would i stop it having previous ect if this isnt filled? The code I posted shouldn't show "Previous" on the first page or "Next" if there are no more images. Ok i have it sorted but there is something i dont know how to do.... how can i display numbers like 12345... at the bottom dependant on how many pages there are? With my code I posted, you would simply do this: $pages = (floor($total/$entriesPerPage)); for($i=0;$i<$pages;$i++) echo "<a href=\"?page=$i\">$i</a> "; Quote Link to comment https://forums.phpfreaks.com/topic/179504-pageination-not-working-right-coping-images-over-4-pages/#findComment-947992 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.