Jump to content

Pagination help


andyd34

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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'},', '));

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.