deansaddigh Posted June 15, 2010 Share Posted June 15, 2010 Can some one see if they can spot any mistakes in my pagination for results page. It only seems to be bringing back 3 results which is incorrect there are more. basically im trying to display my results over several pages. Heres my code // how many rows to show per page - 18 Would equal 3 rows of 6 photos. Below code within while loop specifies the amount of photos per row in the table. $rowsPerPage = 3; // by default we show first page $pageNum = 1; // if $_GET['page'] defined, use it as page number if(isset($_GET['page'])) { $pageNum = $_GET['page']; } // counting the offset $offset = ($pageNum - 1) * $rowsPerPage; $query = "SELECT course.course_id, course.name AS course_name, course.level, course.price, course.info, course_type.type, school.school_id AS school_id, school.name AS school_name, school.street, school.town, school.city, school.county, school.region, school.postcode, school.country, school.school_facts, school.general_info, school.school_facilities FROM school_course JOIN course ON school_course.course_id = course.course_id JOIN school ON school_course.school_id = school.school_id JOIN course_coursetype ON course.course_id = course_coursetype.course_id JOIN course_type ON course_coursetype.course_typeid = course_type.course_typeid WHERE region = '$region' AND course_type.course_typeid ='$coursetype' AND town='$town' AND country='$country' ORDER BY school_name ASC" . " LIMIT $offset, $rowsPerPage"; //Use this query below $result = mysql_query($query, $conn) or die ("Unable to perform query: " . mysql_error()); //echo out all details i want //rest of pagination //******Joes Pagination continued // how many rows we have in database $numrows = $row['numrows']; // how many pages we have when using paging? $maxPage = ceil($numrows/$rowsPerPage); // print the link to access each page $self = $_SERVER['PHP_SELF']; $nav = ''; for($page = 1; $page <= $maxPage; $page++) { if ($page == $pageNum) { $nav .= " $page "; // no need to create a link to current page } else { $nav .= " <a href=\"$self?page=$page\">$page</a> "; } } // creating previous and next link // plus the link to go straight to // the first and last page if ($pageNum > 1) { $page = $pageNum - 1; $prev = " <a href=\"$self?page=$page\">« Prev</a> "; $first = " <a href=\"$self?page=1\">[First Page]</a> "; } else { $prev = ' '; // we're on page one, don't print previous link $first = ' '; // nor the first page link } if ($pageNum < $maxPage) { $page = $pageNum + 1; $next = " <a href=\"$self?page=$page\">Next »</a> "; $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> "; } else { $next = ' '; // we're on the last page, don't print next link $last = ' '; // nor the last page link } echo '<br/>'; echo '<br/>'; //If there is only one page then do nothing. if ($maxPage ==1) { } //Else show user page information and links to browse multiple pages. else { // print the navigation link echo '<p class ="pagination">'; echo $first . $prev . $nav . $next . $last; echo '</p>'; } Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/204870-another-pagination-problem/ Share on other sites More sharing options...
jbrown84 Posted June 15, 2010 Share Posted June 15, 2010 Taking a look at it now, but have you tried echoing the number of rows to the page just so you can check whether it's the query or your pagination logic causing the problem? Quote Link to comment https://forums.phpfreaks.com/topic/204870-another-pagination-problem/#findComment-1072594 Share on other sites More sharing options...
deansaddigh Posted June 16, 2010 Author Share Posted June 16, 2010 hi there, yes i have its the pagination logic most definitely as there are 12 rows that it should be bringing back. Thanks :0 Quote Link to comment https://forums.phpfreaks.com/topic/204870-another-pagination-problem/#findComment-1072862 Share on other sites More sharing options...
deansaddigh Posted June 16, 2010 Author Share Posted June 16, 2010 Im guessing the code is to long for people to try and understand, so im gonna ask another question, does anyone know any easy to implement scripts that will do pagination for me? Quote Link to comment https://forums.phpfreaks.com/topic/204870-another-pagination-problem/#findComment-1072900 Share on other sites More sharing options...
3raser Posted June 16, 2010 Share Posted June 16, 2010 Found this code in my old projects, you may have to edit a little bit... but it's good //max displayed per page $per_page = 8; //get start variable $start = $_GET['start']; //count records $record_count = mysql_num_rows(mysql_query("SELECT * FROM users")); //count max pages $max_pages = $record_count / $per_page; //may come out as decimal if (!$start) $start = 0; //display data $get = mysql_query("SELECT * FROM users ORDER BY id DESC LIMIT $start, $per_page"); while ($row = mysql_fetch_assoc($get)) { //echo out database data //example //echo out all users echo "The user ". $row['user_name'] ." is registered!"; } //setup prev and next variables $prev = $start - $per_page; $next = $start + $per_page; //show prev button if (!($start<=0)) echo "<a href='index.php?start=$prev'>Prev</a> "; //show page numbers //set variable for first page $i=1; for ($x=0;$x<$record_count;$x=$x+$per_page) { if ($start!=$x) echo " <a href='index.php?start=$x'>$i</a> "; else echo " <a href='index.php?start=$x'><b>$i</b></a> "; $i++; Quote Link to comment https://forums.phpfreaks.com/topic/204870-another-pagination-problem/#findComment-1072902 Share on other sites More sharing options...
katierosy Posted June 16, 2010 Share Posted June 16, 2010 Please see http://www.phpbuilder.com/board/showthread.php?t=10283679 and also please read some more examples on paging. To write a much better paging code. Quote Link to comment https://forums.phpfreaks.com/topic/204870-another-pagination-problem/#findComment-1073030 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.