andyd34 Posted March 16, 2009 Share Posted March 16, 2009 I'm haveing a bit of trouble with a results page i'm working on. Iam trying to create the pagination script but, here's the code $recFound = count($IntName); $page_rows = 50; $last = ceil($recFound/$page_rows); if(!$page) { $page = 1; } if($page < 1) { $page = 1; } elseif ($page > $last) { $page = $last; } $max = 'limit ' .($page - 1) * $page_rows .',' .$page_rows; $sqlQry = mysql_query('SELECT * FROM Usr_Profile WHERE Usr_Name IN("'.implode('", "', $IntName).'") '.$max); and the links are if ($page != 1) { $previous = $page-1; ?> <a href="<?=$currentPage?>?page=<?=$previous?>"><strong>Previous</strong></a><? } for($l = 1;$l <= $last;$l++) if($l == $pagenum) { ?> <span style="font-size:14px; margin-top:8px; color:#009999"><strong>[<?=$l?>]</strong></span> <? } else { ?> <a href="<?=$currentPage?>?page=<?=$l?>"><?=$l?></a> <? } ?> <? if ($page!=$last) { $next = $page+1; ?> <a href="<?=$currentPage?>?page=<?=$next?>"><strong>Next</strong></a> <? } the first page loads fine but when you click on a page number its going to the page but your losing the results. the results are in an array $IntName which i can't pass through the link. any suggestions Quote Link to comment https://forums.phpfreaks.com/topic/149724-pagination-help/ Share on other sites More sharing options...
friedemann_bach Posted March 16, 2009 Share Posted March 16, 2009 You could try passing the search term ($IntName) within the "next/previous page" links. Your script already limits the results to the restrictions of the pagination, so this should be the easiest way. Quote Link to comment https://forums.phpfreaks.com/topic/149724-pagination-help/#findComment-786283 Share on other sites More sharing options...
andyd34 Posted March 16, 2009 Author Share Posted March 16, 2009 Thanks for your reply. I have tried the following <a href="<?=$currentPage?>?page=<?=$l?>&intName=<?=$intName?>"><?=$l?></a> <a href="<?=$currentPage?>?page=<?=$l?>&intName=<?=implode(", ", $intName)?>"><?=$l?></a> <a href="<?=$currentPage?>?page=<?=$l?>&intName=<?=implode(",", $intName)"><?=$l?></a> but nothing seems to work Quote Link to comment https://forums.phpfreaks.com/topic/149724-pagination-help/#findComment-786300 Share on other sites More sharing options...
friedemann_bach Posted March 16, 2009 Share Posted March 16, 2009 If I am not mistaken there is a missing "?" in the last row of your script (after the last $intName). When posting any questions on script errors, please do always submit the error message, as it makes things much easier! Quote Link to comment https://forums.phpfreaks.com/topic/149724-pagination-help/#findComment-786302 Share on other sites More sharing options...
andyd34 Posted March 17, 2009 Author Share Posted March 17, 2009 Thanks again for the reply. I have rewrote the search string to make it a bit easier. I have also done this if ($page != 1) { $previous = $page-1; ?> <a href="<?=$currentPage?>?page=<?=$previous?>&results=<?=implode(',',$results)?>"><strong>Previous</strong></a><? } for($l = 1;$l <= $last;$l++) if($l == $page) { ?> <span><strong><?=$l?></strong></span> <? } else { ?> <a href="<?=$currentPage?>?page=<?=$l?>&results=<?=implode(',',$results)?>"><?=$l?></a> <? } ?> <? if ($page!=$last) { $next = $page+1; ?> <a href="<?=$currentPage?>?page=<?=$next?>&results=<?=implode(',',$results)?>"><strong>Next</strong></a> <? } amd when the results are return they are passed through $results = split(",", $_GET{'results'}); this gives Array ( [0] => [1] => eggs [2] => milk [3] => cheese [4] => butter) how do i get rid of [0] => in the array so i am only returning the filled results. Also the the results are showing in the address bar is there anyway to encrypt them Quote Link to comment https://forums.phpfreaks.com/topic/149724-pagination-help/#findComment-786568 Share on other sites More sharing options...
friedemann_bach Posted March 17, 2009 Share Posted March 17, 2009 If I got it right you are trying to pass the whole search result through the URL. To be honest: This is nonsense. Just pass the search term and perform the query again on every page, limited to the results you need. Use "LIMIT <start>,<max_results>", like LIMIT 0,10 to get the first 10 results, then, on the second page, use LIMIT 10,10 to get to the next 10 results and so on (just pass the number of the last row). However, regarding your first question, try implode() instead of split(), this could suit better. You need split() only if you are using regular expressions. And regarding your second question: why would you want to encrypt your results when they are going to be displayed on the page after all? Quote Link to comment https://forums.phpfreaks.com/topic/149724-pagination-help/#findComment-786581 Share on other sites More sharing options...
andyd34 Posted March 17, 2009 Author Share Posted March 17, 2009 Thanks again for your reply, with regards to If I got it right you are trying to pass the whole search result through the URL. To be honest: This is nonsense. Just pass the search term and perform the query again on every page, limited to the results you need. Use "LIMIT <start>,<max_results>", like LIMIT 0,10 to get the first 10 results, then, on the second page, use LIMIT 10,10 to get to the next 10 results and so on (just pass the number of the last row). $results are the end product from a search of 4 tabes (accounts, settings, details and profiles) with the selection dependant on the users chioces. How can I go about getting the page limits for the second page when it doesn't know the criteria unless you add $results to the search string Quote Link to comment https://forums.phpfreaks.com/topic/149724-pagination-help/#findComment-786585 Share on other sites More sharing options...
friedemann_bach Posted March 17, 2009 Share Posted March 17, 2009 Sorry, I think I didn't get the idea of that script - so a user submits some criteria, a search is performed and now the results have to be displayed page by page, right? And all results are of the same type? I think you could just pass all search criteria submitted by the user through the URL without problems. On all next/previous page links, add all criteria and the respective values, as well as the last row of the page (e.g. 10). On the page, just use the submitted search criteria to perform a search, limited to <max_per_page> rows and beginning after <skip_rows>. Set max_per_page to 10 and skip_rows to 0 by default. Lets assume your criteria are <age>, <gender> and <children> and are submitted to your script. Now, on the results page, <max_per_page> is always set to 10. <skip_rows> is set to 0 by default, but can be overridden if another value is submitted via the URL. Now perform the search with your criteria: "SELECT * FROM persons WHERE age=$age AND gender=$gender AND children=$children LIMIT $skip_rows,$max_per_page" and let the results be displayed. Then, add "skip_rows=$skip_rows+$max_per_page" to the next page links, so that on the next page the next 10 results would be displayed, and add "skip_rows=$skip_rows-$max_per_page" to your previous page links. Do not forget to check for negative values. Basically, this is it. Hope this helps? Quote Link to comment https://forums.phpfreaks.com/topic/149724-pagination-help/#findComment-786605 Share on other sites More sharing options...
sasa Posted March 17, 2009 Share Posted March 17, 2009 Thanks again for the reply. I have rewrote the search string to make it a bit easier. I have also done this if ($page != 1) { $previous = $page-1; ?> <a href="<?=$currentPage?>?page=<?=$previous?>&results=<?=implode(',',$results)?>"><strong>Previous</strong></a><? } for($l = 1;$l <= $last;$l++) if($l == $page) { ?> <span><strong><?=$l?></strong></span> <? } else { ?> <a href="<?=$currentPage?>?page=<?=$l?>&results=<?=implode(',',$results)?>"><?=$l?></a> <? } ?> <? if ($page!=$last) { $next = $page+1; ?> <a href="<?=$currentPage?>?page=<?=$next?>&results=<?=implode(',',$results)?>"><strong>Next</strong></a> <? } amd when the results are return they are passed through $results = split(",", $_GET{'results'}); this gives Array ( [0] => [1] => eggs [2] => milk [3] => cheese [4] => butter) how do i get rid of [0] => in the array so i am only returning the filled results. Also the the results are showing in the address bar is there anyway to encrypt them $results = split(",", trim($_GET{'results'},', ')); Quote Link to comment https://forums.phpfreaks.com/topic/149724-pagination-help/#findComment-786615 Share on other sites More sharing options...
kickstart Posted March 17, 2009 Share Posted March 17, 2009 Hi As above for the criteria. However if the generation of the results is very complex and time consuming then consider saving the results into an extra table, so when paging forward you are just doing a simple select (but you would need some housekeeping on the table). However I would only consider this if the initial generation of results is very complex (ie, might well use it if each line of data was taken from a seperate database and then the results sorted) All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/149724-pagination-help/#findComment-786617 Share on other sites More sharing options...
andyd34 Posted March 19, 2009 Author Share Posted March 19, 2009 I managed to get it sorted. $search = 'page='.$page; $search .= 'field1='.$field1; $search .= 'field2='.$field2; $search .= 'field3='.$field3; $search .= 'field4='.$field4; and so on then <a href='<?=$_SERVER['PHP-SELF']?>?<?=$search?>'>Next Page</a> seemed to do the trick and is working fine Quote Link to comment https://forums.phpfreaks.com/topic/149724-pagination-help/#findComment-788875 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.