Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing form selections with query string

Status
Not open for further replies.

ksynergy

Programmer
Oct 10, 2007
5
US
Hello,
I have been working on a site that is now using dropdown lists that allow multiple selections. When the form is submitted, the dropdown selections are converted to lists via code like this:
$areas = implode("','",$_POST['area']);

Then in a mySQL query, the value of $areas is used like this:
AND Area IN('$areas')";

This works fine to bring back query results filtered by the values in the $areas list.

But if there are a lot of results, the first page displays only 10 of them. Previous/next code was in place to allow navigation to other pages and also provided numbers for the pages and those could be used as well. These navigation links work by passing the query criteria as query string values. However, trying to pass the value of $area which may contain multiple values from the dropdown list sends only the string 'array' and the page fails when it reloads. The value of $area is no longer useful.

Does anyone know how to pass the multiple values from the dropdown selection into the navigation query string for navigation to another set of results?
 
you need to show us more code. the string Array is being passed because at some point you are not correctly imploding the array.
 
Okay. Here is the code:

This checks the drop down list named 'area'
if($area){
$areas = implode("','",$_POST['area']);
if($areas != "ANY")
{
$area_clause = "AND Area IN('$areas')";
}
else
{
$area_clause = "AND Area = Area";
}
}

Here is an example query that will return results:

$numresults = mysql_query("SELECT MLS, Price, Type, Remarks FROM $type WHERE (Type = 'RESIDENTIAL WATERFRONT') AND (Price >= $lorange AND Price <= $hirange) AND (Status = 'Active') $area_clause"); // the query.

Here is the link for the next page when more than 10 results are returned:

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

The first page of results is correct, but the next page (same page reloaded) returns no results because the $area value no longer returns a list when imploded. It seems to just be a string value equal to 'Array'.
 
Haven't worked in PHP much and perhaps I'm misunderstanding how a form value like $area is handled on the page when attempting to pass it as part of a query string. If I implode the value and create the value $areas, I get the comma delimited string and can use it in the query, but didn't want to pass the whole string of perhaps 3 or 4 values as a list in the string for use when navigating further. Not even sure that would work, but maybe that's what is needed?
 
your problem is in your link. instead of storing the stuff in the string consider using sessions instead and keep the value as an array until you are ready to implode it in the query.

Code:
session_start();
if (isset($_SESSION['area'])){
  if (isset($_POST['area'])){
    $_SESSION['area'] = array_merge($_SESSION['area'], $_POST['area']);
  }
}

//when ready for the query
$area = "'".implode ("','",$_SESSION['area'])."'";

the last implode does not assume that the areas are all integers. it therefore enquotes all array elements. this, even then, is not a great solution as you should escape them too. in which case use this code instead

Code:
function prepareIn($array){
 $output =' '; //note space to make the substr work ok with nil returns
 foreach($array as $value){
  $output .= "'".mysql_escape_string($value)."',";
 }
 return substr($output, 0, -1);
}
 
or you could always use the inlink method and serialise and urlencode the array. i prefer the session method myself
 
Thanks for the help. I will see if I can fix things. I've worked with session values in the past with PHP, but it's been awhile since I don't use PHP much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top