squiblo Posted September 7, 2009 Share Posted September 7, 2009 below is a search engine script and im tring to improve it by adding pagination to it, but im having a little problem with the "next" button, i have looked at other script but i cant understand them as i am new to php and i want to make one that i can understand, the "prev" button does work. there are no errors within the script but my problem is that the "next" button is never displayed. please help. <?php //connect to our database mysql_connect("","",""); mysql_select_db(""); //strlen check $search = $_GET['results']; if(strlen($search)<3) echo "Your search must be at least 3 characters long."; //results per page $per_page = 10; //get start variable $start = $_GET['start']; //get start if(!$start) $start = 0; //explode our search term $search_exploded = explode(" ",$search); foreach($search_exploded as $search_each) { //construct query $x++; if ($x==1) $query .= "description LIKE '%$search_each%' LIMIT $start, $per_page"; else $query .= " OR description LIKE '%$search_each%' LIMIT $start, $per_page"; } $query = "SELECT * FROM members WHERE $query"; //count results $run = mysql_query($query); $foundnum = mysql_num_rows($run); //count max pages $max_pages = $foundnum / $per_page; //may come out as a decimal if ($foundnum==0) echo "You searched for <b>$search</b>. No results found."; else { echo "You searched for <b>$search</b><br>$foundnum result(s) found!<p><hr size='1' width='387'color='#E6E6E6' align='left'>"; while ($runrows = mysql_fetch_assoc($run)) { //get data $city = ucwords($runrows['city']); $pageurl = $runrows['pageurl']; $company = ucwords($runrows['company']); $logo = $runrows['logo']; $pageid = $runrows['pageid']; if ($imagelocation == "") { $imagelocation = "./profileimages/noprofilepic.jpg"; } echo " <img src ='$logo' width='100' height='105' border='0' align='left' style='padding-right:10px'><br> <b>$company</b><br> $city<br> <a href='http://www.squiblo.com/page.php?pageid=$pageid' style='text-decoration:none;'><font color='#FFFF00'><u>View page</u></font></a><br><br><br><br><br> <hr size='1' width='387' align='left' color='#E6E6E6'> "; } } //navigation //previous and next variables $prev = $start - $per_page; $next = $start + $per_page; //show prev button if (!($start<=0)) echo "<a href='results2.php?start=$prev&results=$search'>Prev</a> "; //show page numbers (not done yet) //show next button if (!($start>=$foundnum-$per_page)) echo " <a href='results2.php?start=$next&results=$search'>Next</a>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/173457-next-page-for-pagination/ Share on other sites More sharing options...
TeNDoLLA Posted September 7, 2009 Share Posted September 7, 2009 You have few bugs also in you code, for example what If user enters in the browser as a start -20 and not through your buttons? You let it all go which will lead to that your mysql query will fail. I am not following the logic all the time but I made quickly a example paging how I would do it. It's simple and it works. Maybe you can get some help examining it. <?php // Entries per page. $perPage = 4; // Get max count for max pages. $countSql = "SELECT count(id) AS amount FROM koirat"; $countResult = mysql_query($countSql); $count = mysql_fetch_object($countResult); $count = $count->amount; // Calculate max pages. $maxPages = floor($count/$perPage); // Check user inputted page does not exceed maxPages and is over zero. if (isset($_GET['page']) && intval($_GET['page']) > 0 && intval($_GET['page']) <= $maxPages) { $page = intval($_GET['page']); } else { $page = 1; } // Calculate start (and offset = $perPage). $start = ($page * $perPage) - $perPage; // Build query, run and get results for current page. $sql = "SELECT * FROM koirat LIMIT $start, $perPage"; $result = mysql_query($sql); while ($row = mysql_fetch_object($result)) { echo $row->id . ' - ' . $row->nimi . '<br/>'; } // Print prev button. echo ($page > 1) ? '<a href="?page=' . ($page - 1) .'">prev</a>' : 'prev '; // Print pages for ($i=1; $i<=$maxPages; $i++) { echo ($i === $page) ? $i .' ' : '<a href="?page='. $i .'">'. $i .'</a> '; } // Print next button. echo ($page < $maxPages) ? ' <a href="?page=' . ($page + 1) .'">next</a>' : ' next'; Quote Link to comment https://forums.phpfreaks.com/topic/173457-next-page-for-pagination/#findComment-914353 Share on other sites More sharing options...
TeNDoLLA Posted September 7, 2009 Share Posted September 7, 2009 Forgot to mention that if you use some search criteria as you do, you have to add the where part also to the $countSql query. Quote Link to comment https://forums.phpfreaks.com/topic/173457-next-page-for-pagination/#findComment-914360 Share on other sites More sharing options...
squiblo Posted September 7, 2009 Author Share Posted September 7, 2009 doing a pagination script that is just reading from a database im ok with and im ok with a search engine script but putting them both together really confuses me. TeNDoLLA, is it ok if you could give an example with my search paginated and i could try and change a few things? Quote Link to comment https://forums.phpfreaks.com/topic/173457-next-page-for-pagination/#findComment-914364 Share on other sites More sharing options...
TeNDoLLA Posted September 7, 2009 Share Posted September 7, 2009 Now I modified the code I posted earlier so that it has search and the search is remembered while changed pages. <?php // Search was made. if (isset($_POST['search']) && strlen(trim($_POST['search']))) { $search = $_POST['search']; // For keeping search while changing pages. $where = " WHERE nimi LIKE '%" . trim($_POST['search']) ."%' "; $page = 1; // Page must be set to 1 after new search. } else if (isset($_GET['search'])) { $search = $_GET['search']; // For keeping search while changing pages. $where = " WHERE nimi LIKE '%" . trim($_GET['search']) ."%' "; } else { $where = ''; $search = ''; } // Entries per page. $perPage = 4; // Get max count for max pages. $countSql = "SELECT count(id) AS amount FROM koirat $where"; $countResult = mysql_query($countSql); $count = mysql_fetch_object($countResult); $count = $count->amount; // Calculate max pages. $maxPages = floor($count/$perPage); // Check user inputted page does not exceed maxPages and is over zero. if (isset($_GET['page']) && intval($_GET['page']) > 0 && intval($_GET['page']) <= $maxPages) { $page = intval($_GET['page']); } else { $page = 1; } // Calculate start (and offset = $perPage). $start = ($page * $perPage) - $perPage; // Build query, run and get results for current page. $sql = "SELECT * FROM koirat $where LIMIT $start, $perPage"; $result = mysql_query($sql); while ($row = mysql_fetch_object($result)) { echo $row->id . ' - ' . $row->nimi . '<br/>'; } // Print prev button. echo ($page > 1) ? '<a href="?page=' . ($page - 1) . (strlen($search) ? '&search=' . $search : '') .'">prev</a> ' : 'prev '; // Print pages for ($i=1; $i<=$maxPages; $i++) { echo ($i === $page) ? $i .' ' : '<a href="?page='. $i . (strlen($search) ? '&search=' . $search : '') .'">'. $i .'</a> '; } // Print next button. echo ($page < $maxPages) ? ' <a href="?page=' . ($page + 1) . (strlen($search) ? '&search=' . $search : '') . '">next</a>' : ' next'; ?> <p> <form action="FAST_PAGING.php" method="POST"> <input type="text" name="search" /><br/> <input type="submit" value="search" /><br/> </p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/173457-next-page-for-pagination/#findComment-914378 Share on other sites More sharing options...
squiblo Posted September 7, 2009 Author Share Posted September 7, 2009 everything is working nearly perfect, but when i am searching, and i use more that one word in my search, no results are found Quote Link to comment https://forums.phpfreaks.com/topic/173457-next-page-for-pagination/#findComment-914393 Share on other sites More sharing options...
squiblo Posted September 7, 2009 Author Share Posted September 7, 2009 just found another problem, changing the results per page to more than 4 does not show all of my results, im just entered 12 rows of dummy data in my database and only 10 show when i change the results per page to more than 4 Quote Link to comment https://forums.phpfreaks.com/topic/173457-next-page-for-pagination/#findComment-914406 Share on other sites More sharing options...
squiblo Posted September 7, 2009 Author Share Posted September 7, 2009 ive modified the code slightly here is it... <html> <body> <p> <form action="resultstest.php" method="GET"> <input type="text" name="search" /><br/> <input type="submit" value="search" /><br/> </p> </form> </body> </html> <?php //connect to our database mysql_connect("","",""); mysql_select_db(""); // Search was made. if (isset($_POST['search'])) { $search = $_POST['search']; // For keeping search while changing pages. $where = " WHERE description LIKE '%" . ($_POST['search']) ."%' "; $page = 1; // Page must be set to 1 after new search. } else if (isset($_GET['search'])) { $search = $_GET['search']; // For keeping search while changing pages. $where = " WHERE description LIKE '%" . ($_GET['search']) ."%' "; } else { $where = ''; $search = ''; } // Entries per page. $perPage = 4; $query = "SELECT * FROM members $where"; //count results $run = mysql_query($query); $foundnum = mysql_num_rows($run); //count max pages $maxPages = floor($foundnum / $perPage); //may come out as a decimal // Check user inputted page does not exceed maxPages and is over zero. if (isset($_GET['page']) && intval($_GET['page']) > 0 && intval($_GET['page']) <= $maxPages) { $page = intval($_GET['page']); } else { $page = 1; } // Calculate start (and offset = $perPage). $start = ($page * $perPage) - $perPage; // Build query, run and get results for current page. $sql = "SELECT * FROM members $where LIMIT $start, $perPage"; $result = mysql_query($sql); while ($row = mysql_fetch_object($result)) { echo $row->id . ' - ' . $row->description . '<br/>'; } // Print prev button. if (!($start<=0)) echo ($page > 1) ? '<a href="?page=' . ($page - 1) . (strlen($search) ? '&search=' . $search : '') .'">prev</a> ' : 'prev '; // Print pages for ($i=1; $i<=$maxPages; $i++) { echo ($i === $page) ? $i .' ' : '<a href="?page='. $i . (strlen($search) ? '&search=' . $search : '') .'">'. $i .'</a> '; } // Print next button. if (!($start>=$foundnum-$perPage)) echo ($page < $maxPages) ? ' <a href="?page=' . ($page + 1) . (strlen($search) ? '&search=' . $search : '') . '">next</a>' : ' next'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/173457-next-page-for-pagination/#findComment-914408 Share on other sites More sharing options...
TeNDoLLA Posted September 7, 2009 Share Posted September 7, 2009 Maybe you have run some search somewhere between? The search is not reseted whie changing pages. You should modify the code a little if you want the search to reset everytime you change page (which makes no sense to me). This can be also achieved with searching just an empty string. Or maybe create a reset button or something like that. Edit: Or there is a bug in your code. Quote Link to comment https://forums.phpfreaks.com/topic/173457-next-page-for-pagination/#findComment-914410 Share on other sites More sharing options...
squiblo Posted September 8, 2009 Author Share Posted September 8, 2009 change $maxPages = floor($count/$perPage); to $maxPages = ceil($count/$perPage); Quote Link to comment https://forums.phpfreaks.com/topic/173457-next-page-for-pagination/#findComment-914421 Share on other sites More sharing options...
TeNDoLLA Posted September 8, 2009 Share Posted September 8, 2009 You are now mixing two different method together. This is piece of code... <?php // Print prev button. if (!($start<=0)) echo ($page > 1) ? '<a href="?page=' . ($page - 1) . (strlen($search) ? '&search=' . $search : '') .'">prev</a> ' : 'prev '; ...same as this code: <?php // Print prev button. if (!($start<=0)) if ($page > 1) { $prev = '<a href="?page=' . ($page - 1); if (strlen($search)) { $prev .= '&search=' . $search; } $prev .= '">prev</a> '; } else { $prev = 'prev '; } echo $prev; Which makes no sense, or does it? Edit: btw. my bad. You are right it was supposed to be ceil() not floor() when counting max pages! Quote Link to comment https://forums.phpfreaks.com/topic/173457-next-page-for-pagination/#findComment-914431 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.