hammerman Posted January 27, 2008 Share Posted January 27, 2008 Hi im trying to paginate search results, if the user submits a couple of search criteria from drop down boxes, then the script brings up relevant results. I have been able to set up the script so that it will show links "prev" and "next" if there is more than one page of results, but i cant get the script right so that "next" will show the second lot of results, or "prev" for that matter. I dont think you will want the whole page so i have pulled out what i think is relevant <?php ////// headers $albumPerPage = 10; $pageNumber = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1; $offset = ($pageNumber - 1) * $albumPerPage; $serial = $offset + 1; /////part of the query $result = mysql_query($query . "LIMIT $offset, $albumPerPage") or die (mysql_error()); $num=mysql_num_rows($result); ///// at the bottom of the script to recall the pagination script $result = mysql_query($query); $totalResults = mysql_num_rows($result); echo getPagingLink($totalResults, $pageNumber, $albumPerPage, "page=search"); ?> The search drop downs and search result scripts are all on the one page search.php, so if you click on the "next" or "prev" links it just goes back to showing the drop down boxes and not the results. I cant think of what to put in the "page=search" part to bring up the next lot of results. Ive looked through the tutorial and other threads but none of them solve this problem as far as i can see. Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 27, 2008 Share Posted January 27, 2008 Firstly, welcome to the forum. Second, we're going to need to see far more of the code than that. Showing up the getPagingLink() function would be a good place to start, along with the links it generates Without any more, i can only guess at what the problem is. And my guess would be that you're only passing the page number and not passing the search criteria in the URL. Therefore, when the page loads, the search criteria are not set, so you are showing the search form. p.s. Try and remember to use the tags Quote Link to comment Share on other sites More sharing options...
hammerman Posted January 28, 2008 Author Share Posted January 28, 2008 Thanks for the welcome and reply. here is the getPagingLink() function, i hope its right. i can give you the code to the search.php but it will be quite long, would phpfreaks accept that? function getPagingLink($totalResults, $pageNumber, $itemPerPage = 10, $strGet = '') { $pagingLink = ''; $totalPages = ceil($totalResults / $itemPerPage); $numLinks = 10; if ($totalPages > 1) { $self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ; if ($pageNumber > 1) { $page = $pageNumber - 1; if ($page > 1) { $prev = " <a href=\"$self?pageNum=$page&$strGet\">[Prev]</a> "; } else { $prev = " <a href=\"$self?$strGet\">[Prev]</a> "; } $first = " <a href=\"$self?$strGet\">[First]</a> "; } else { $prev = ''; $first = ''; } if ($pageNumber < $totalPages) { $page = $pageNumber + 1; $next = " <a href=\"$self?pageNum=$page&$strGet\">[Next]</a> "; $last = " <a href=\"$self?pageNum=$totalPages&$strGet\">[Last]</a> "; } else { $next = ''; $last = ''; } $start = $pageNumber - ($pageNumber % $numLinks) + 1; $end = $start + $numLinks - 1; $end = min($totalPages, $end); $pagingLink = array(); for($page = $start; $page <= $end; $page++) { if ($page == $pageNumber) { $pagingLink[] = " $page "; page } else { if ($page == 1) { $pagingLink[] = " <a href=\"$self?$strGet\">$page</a> "; } else { $pagingLink[] = " <a href=\"$self?pageNum=$page&$strGet\">$page</a> "; } } } $pagingLink = implode(' | ', $pagingLink); // return the page navigation link $pagingLink = $first . $prev . $pagingLink . $next . $last; } return $pagingLink; } ?> thanks Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 29, 2008 Share Posted January 29, 2008 Right, well it looks to me like the links are being generated correctly. Therefore, im going to go with my above statement: My guess would be that you're only passing the page number and not passing the search criteria in the URL. Therefore, when the page loads, the search criteria are not set, so you are showing the search form. Perhaps you do need to be passing page=search through the url; i do not know, since i do not know how the rest of your script works. However, what you certainly need to be doing is passing the query you have made to the database through the URL (you will probably want to just pass the where clause and any order by clause). You will then only want to show the search form if the URL does not contain the database query. When you say it's quite long, if we're talking a few hundred lines, then you may as well post it up if you get stuck. Quote Link to comment Share on other sites More sharing options...
hammerman Posted January 30, 2008 Author Share Posted January 30, 2008 Hi sorry, i dont know how to send the "page=search.php" results to the url. Ive posted up all my code now, the following one is main.php which allows me to call up different pages, the initial one being search.php. <?php include 'library/config.php'; include 'library/functions.php'; $page = (isset($_GET['page']) && $_GET['page'] != '') ? $_GET['page'] : 'search'; $allowedPages = array('search, list, info, contact'); if (!in_array($page, $allowedPages)) { $page = 'notfound'; } ?> <html> <head> <link rel="stylesheet" type="text/css" href="gallery.css"> </head> <body link="blue" alink="blue" vlink="blue" background="#a4d8ff"> <a href="index.php"><img src="head.gif" width="100%"></a> <p> </p> <table bgcolor="" width="100%"> <tr> <td bgcolor="#9595ff" width="20%" align="center"><a class="ex2" href="info.php"><b>info pages</b></a></td> <td bgcolor="#9595ff" width="20%" align="center"><a class="ex3" href="article?page=contact"><b>contact us</b></a></td> <td bgcolor="#9595ff" width="20%" align="center"><a class="ex4" href="article?page=photos"><b>photos</b></a></td> </tr> </table> <br> <table width="750" border="0" align="center" cellpadding="2" cellspacing="1" class="table_main"> <tr> <th> Location : <a class="ex6" href="index.php">Home</a> <?php echo showBreadcrumb(); ?> </th> </tr> <tr> <td> <?php include $page. '.php'; ?> </td> </tr> </table> </body> </html> This next set is the search.php script, ive cut down the dropdown boxes simply for ease. Thanks for all your help. <html> <body> <?php $albumPerPage = 10; $pageNumber = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1; $offset = ($pageNumber - 1) * $albumPerPage; $serial = $offset + 1; include 'library/connect.php'; error_reporting(E_ALL); if (!isset($_GET['Submit'])) { ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="get"> <select size="1" name="dropdown"> <option value=" %" selected>all</option> <option value="%london">london</option> <option value="%manchester">rmanchester</option> </select> <select size="1" name="dropdown1"> <option value=" %" selected>both</option> <option value=" %male">male</option> <option value="%female">female</option> </select> <select size="1" name="dropdown2"> <option value=" %" selected>all</option> <option value=" 20">under 20yrs</option> <option value=" 30">under 30yrs</option> <option value=" 70">under 70yrs</option> </select> <input type="Submit" value="Submit" name="Submit"> </form> <?php } else { $dropdown = mysql_escape_string($_GET['dropdown']); $dropdown1 = mysql_escape_string($_GET['dropdown1']); $dropdown2 = mysql_escape_string($_GET['dropdown2']); $album = isset($_GET['dropdown']) ? $_GET['dropdown'] : ''; $album1 = isset($_GET['dropdown1']) ? $_GET['dropdown1'] : ''; $album2 = isset($_GET['dropdown2']) ? $_GET['dropdown2'] : ''; $album3 = isset($_GET['Submit']) ? $_GET['Submit'] : ''; $connect = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Unable to connect to host"); mysql_select_db($dbname) or die ("Unable to connect to database"); $query = "SELECT * FROM ideas WHERE city LIKE '$dropdown' AND sex LIKE '$dropdown1' AND price<='$dropdown2' AND approved='y'" or die (mysql_error()); $result = mysql_query($query . "LIMIT $offset, $albumPerPage") or die (mysql_error()); $num=mysql_num_rows($result); echo "<b><center>heres your search results</center></b><br><br>"; if ($num == 0) { echo "Sorry there are no results for those specs"; } else { $serial = $offset + 1; while ($row = mysql_fetch_assoc($result)) { extract($row); ?> <table border=0 cellspacing=0 cellpadding=0 width="100%"> <tr> <th width="200" align="center">image</th> <th width="200" align="center">city</th> <th width="200" align="center">sex</th> <th width="200" align="center">age</th> </tr> <?php $i=0; while ($i < $num) { $z=mysql_result($result, $i, "image"); $a=mysql_result($result, $i, "city"); $b=mysql_result($result,$i,"sex"); $c=mysql_result($result,$i,"age"); $i++; ?> <tr> <td with="200"><b><image width="50" src="images/album/<?php echo $z; ?>"><b></td> <td with="200"><b><?php echo $a; ?><b></td> <td with="200"><b><?php echo $b; ?><b></td> <td with="200"><b><?php echo $c; ?><b></td> </tr> <?php } } $result = mysql_query($query); $totalResults = mysql_num_rows($result); echo getPagingLink($totalResults, $pageNumber, $albumPerPage, "page=search"); } } ?> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
hammerman Posted February 4, 2008 Author Share Posted February 4, 2008 If anyone could help me, ive been told that i need to send my search through the url, for the script above, in order to paginate it but i dont know how to do this- ive looked but cannot find any pages to help me send multiple drop down boxes... Quote Link to comment Share on other sites More sharing options...
hammerman Posted February 10, 2008 Author Share Posted February 10, 2008 ok i understand what is going wrong but i dont know how to correct it... The search is being sent to the url but the pagination problem comes with the LIKE statement, the pagination will work if i use dropdown=$dropdown but not if i use dropdown LIKE $dropdown in the mysql query. However, i need to use the LIKE statement in order to use the wild card % to retrieve every row if nothing is specified. Is there any other way of doing this or how would i input the LIKE into the "page=" of the getpaginglink function? Any help would be greatly appreciated Quote Link to comment 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.