Link to home
Start Free TrialLog in
Avatar of Adam
AdamFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do I pass on received search parameters within my pagination code?

Hello all.

I am trying to pass on parameters contained in my initial search form, onto the pagination script in my results page.  I'm not sure how to do this properly.

My set up currently is flawed in that I lose my initial search criteria when I click on the [Next], [Last] etc buttons in my pagination script.  I guess this is because I need to pass the parameter values from my initial search into the pagination? Currently my simple pager code looks like this:

<ul class="pagination justify-content-end">
					  
					 <?php
					 if($pageno <= 1){?>
						<li class="page-item disabled"><a class="page-link" href="#">&#x276e; &#x276e; First </a></li>
					<?php } ?>

					<?php
					if($pageno > 1){?>
						<li class="page-item"><a class="page-link" href="?pageno=1"> &#x276e; &#x276e; First </a></li>
					<?php } ?> 
					  
					  
					  <li class="page-item"><a class="page-link" href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>"> &#x276e; Previous</a></li>
					  
					  <li class="page-item"><a class="page-link" href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>"> Next &#x276f;</a></li>
					  
					 
					 <?php
					 if($pageno >= $total_pages){?>
						<li class="page-item disabled"><a class="page-link" href="#">Last &#x276f; &#x276f;</a></li>
					<?php } ?>

					<?php
					if($pageno < $total_pages){?>
						<li class="page-item"><a class="page-link" href="?pageno=<?php echo $total_pages; ?>">Last &#x276f; &#x276f;</a></li>
					<?php } ?> 
					  
				</ul>

Open in new window


In my initial search box I post to the results page. There are 4 parameters which I define at the top of my results page:

 <?php
  
  $language   = isset($_POST['language'  ]) ? $_POST['language'  ] : null; //These are the values sent through from the simple search box on the homepage
  $prefecture = isset($_POST['prefecture']) ? $_POST['prefecture'] : null;
  $vid        = isset($_POST['vid'       ]) ? $_POST['vid'       ] : null;
  $photo      = isset($_POST['photo'     ]) ? $_POST['photo'     ] : null;
  
  
  $whereClause = []; //we are creating an empty array and calling it '$whereClause'
  
  //The 'mysqli_real_escape_string()' function escapes special characters in a string for use in an SQL statement.
  //It provides a level of protection against SQL Injection
  
  if ($language) {  
     $whereClause[] = 'language = "'. mysqli_real_escape_string($db_connection, $language) .'"';  //if a value for language has been passed in then add it to the array
 }
 
 if ($prefecture) {
     $whereClause[] = 'prefecture = "'. mysqli_real_escape_string($db_connection, $prefecture) .'"';
 }
 
 if ($photo) {
     $whereClause[] = 'photo = "1"';
 }
  
  if ($vid) {
     $whereClause[] = 'vid = "1"';
 }
 
 if (empty($whereClause)) {
     $whereClause[] = '1';
 }
 
 ?>

Open in new window


In my SQL statement to display the results I used  [ . join(' AND ', $whereClause) ."  " ] to pass on the entered parameters and I guess I need to do something similar in my pager script.  As I vaguely understand it, $whereClause is an array which stores all the values contained within the original search, and I want results from the pagenation only to display these results. I think? So I'm thinking somehow use .$whereClause? I tried messing around with this but no success.

Any help on how to do this (hopefully I'm just a few lines away from getting it to work) would be great.

Many thanks for reading.

Adam

P.S. Happy to post more code it it helps.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adam

ASKER

Hi Chris.

Many thanks for the response. When I first saw the post before going out yesterday, I assumed that the code you posted was only part of the solution and I would still have to amend the pagination links with the $_SESSION variable (somehow linking back to the current page). I started trying to figure out the correct code to do this, this morning, but after about an hour thought - 'I better test this first with only Chris' code to see what the error message is'.

Wonderful surprise when I saw it working without any amendment to the pagination links.

I'm not the sharpest egg in the box, but is this because we're not actually linking to another page (just a page number) and also because the SESSION VALUE is stored within the $whereClause array?

Anyway, many thanks in helping me yet again. Works just as hoped.
Avatar of Adam

ASKER

Super fast response and the solution on plate. Many thanks Chris Stanyon for helping me again. When my website finally goes live it will be in a large part thanks to your help.
No worries Adam. Glad you got it working.

A SESSION is a server storage system, so basically when we do a search (by submitting the search form) we save the search terms in the SESSION on the server. Now any page on your website can retrieve those stored values from the server's SESSION. This means when you paginate through your pages, you can just grab the search values that have been saved to the SESSION and rebiuld your $whereClause by using those values.

With the pagination links, you are actually linking to a page. By having a link like this: ?pageno=3, that automatically get's translated as yourcurrentpage.php?pageno=3

Hope that all makes sense and good luck with your project :)