Jump to content

Recommended Posts

Hello,

I'm new to PHP and having trouble with a search function. Users start with a page where they pick criteria in a form to use in a search for real estate properties. Some of the criteria are in drop down lists and users can pick more than one item from the list.

 

When they click the submit button, the selections are passed as form values. The values from the drop down lists are currently handled on the results page with code like the following:

 

if($area){

$areas = implode("','",$_POST['area']);

if($areas != "ANY")

{

$area_clause = "AND Area IN('$areas')";

}

else

{

$area_clause = "AND Area = Area";

}

}

 

If the user didn't make any selection, the value from the drop down is "ANY". If they did make a selection, then a list of one or more items is created as $areas. The 'area_clause' is used in a mySQL query to return results.

 

This works fine on the first page of results, but in many cases there will be multiple pages of results returned and the page is configured to display only 10 rows at a time. The problem I'm having is figuring out how to get the criteria from the drop down lists passed to pages 2, 3, 4, etc. The results page is always the same page, just getting a different set of rows based on which page it is in the total number. So the value of $area needs to be passed multiple times as they navigate through pages. But the form value only exists the first time. If I just use something like the following, the drop down list for $area is only passed as "Array" to the next page, not the list:

 

<a href=\"$PHP_SELF?query=$query&page=$next_page&area=$area&limit=$limit&type_search=$type_search&price_search=$price_search\">next</a>

 

 

I have searched without success for examples in which drop down criteria can be provided to a single search page while going through multiple rows of records from a query. If I could display all results in a single page it would be relatively easy, but a page with 75 or 100 rows of results isn't very friendly. Is there any way to keep the search criteria available for the query for each new page of results? It would be great if it kept working like it does on the first page of results, but I haven't figured out how to do that and get the next page to work in the same way.

 

Thanks for any help.

Link to comment
https://forums.phpfreaks.com/topic/73268-solved-multi-page-search/
Share on other sites

well, this is called pagination. there is a tutorial on phpfreaks about pagination...check this out...http://www.phpfreaks.com/tutorial_cat/25/Page-Number--Pagination.php...there are two tutorials...ok,  i wrote a pagination class which u can use, all you have to do is something like the below:

 

// include the pagination class script
require("paginateclass.php");
// connect to mysql and select the database
$con=mysql_connect("localhost", "***", "***");
$db=mysql_select_db("pagin", $con);
// get needed results
$que="select name from users";
$res=mysql_query($que);
// put results into $res2 array
while ($row=mysql_fetch_array($res))
{
$res2[]=$row[0];
}
/* create a pagination object, the first argument is the array which has the results, the second is how many results you want to display, and the page you want the results displayed, note that i added ? add the back because i'm passing the page no as in the url, e.g., example.php?page=1*/

$pagin=new Pagination($res2, 10, $_SERVER["PHP_SELF"]."?");

/* then, call paginate function, the first argument is the page no., if u want to display page numbers, second argument is 1, else 0,  if u want to display prev and next links, second argument is 1, else 0
$pagin->paginate($_GET["page"], 1, 1);*/

 

if u want the class, pm me....quick

Thanks for the reply. I'm new to this forum and currently getting an error message that says 'not allowed to send PM'. But in any case, the class might be what I need. I wasn't having any trouble with pagination until I was asked to add the multi-select drop down lists. Prior to that they had all been single selection combo boxes and I could easily pass the selected values from page to page.

 

Based on your post, it seems like the class and your guidance would help me figure this out. My email is ken.eck1@gmail.com and I would really appreciate getting the class.

After reading the post from JD2007, I realized that he/she was probably misunderstanding the problem. I don't have a problem with pagination per se - I can get the query results to display in multiple pages without any problem. The problem is that some of the criteria to be used in the query have gone from being single value variables to multiple values (select more than one from a drop down list). I haven't been able to figure out how PHP can be used to pass these arrays from one page to the next. In the query string attached to the pagination URL, the $area value seems to end up being the string value "Array", not the actual Array that was passed from the search form at the start. So when I get to page 2, I no longer have the $area as a variable I can use in the query.

 

I don't have much experience with PHP and I'm having no luck so far finding the combination of functions and techniques to make this work. Anyone else who can offer some guidance?

Wait, is it one value from a dropdown list or is it a <select MULTIPLE> type list to select multiple options at once?

 

If it's just one value from a dropdown list, I don't see why you need to make it an array... Perhaps you can show me the html form for this thing so I can help? I'm pretty good with pagination.

Hi. It is a <select multiple>. It started out as a single select, but then they changed their minds. When I was asked to change it, I learned that I could use the implode function to take the submitted form value and convert it to a comma delimited list. See the code snip in the original post. This works fine on returning results on the first page.

 

Then I discovered it wasn't so easy to keep passing this multiple select value to subsequent pages. Just placing the original value $area in the query string used for paging didn't work since I ended up with a string that simply said 'array'. I'm guessing that the value needs to be saved in some fashion that will work in the query string since it will no longer be a $POST value. But I don't have the PHP experience to know how to do that.

 

Let me know if you need to see more code than is posted in the original message. Thanks.

This one seems to keep getting lost in the shuffle. Still looking for some guidance on how to pass a multiple select value to subsequent pages in a multi-page situation. The value is available as a $_POST value on the first page of results, but I don't know how to pass it to the next page via a query string in the URL. As a result, the second page of results from the query doesn't return any records.

 

I tried posting this question to another forum, but all I got back was an acknowledgement that it was a good question that someone else would like answered too. Not much help there.

 

Anyone else have any suggestions??

As a workaround why not try looping through your array and building the $area_clause like:

 

$area_clause = "AND (";

foreach($_POST['area'] as $value)
  {
   $area_clause .= 'Area = ' .$value . ' OR ';
  }

$area_clause = substr($area_clause,0,-4); //remove the last OR 

$area_clause .= ")";

 

Then storing it in a session which you can retrieve on the next page

 

Or you could pass the new $area_clause in the url

 

Just an idea but should solve your problem.

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.