Jump to content

Using foreach to print out arrays to form a search query


harold

Recommended Posts

Hi,

I try to use foreach to print out a part of an mysql query, but always the last one cancels the previous one out. Please advise me on what to do:

 

$searchterm=$_POST['searchterm'];

 

 

if ($type) {

foreach($_POST['type'] as $type) {

$search_type = "AND type = '$type' ";

}

} else {

$search_type = " ";

}

 

if ($year) {

foreach($_POST['year'] as $year) {

$search_year = "AND DATE_FORMAT(dateofissue,'%Y') = '$year' ";

}

} else {

$search_year = "  ";

}

 

if ($theme) {

foreach($_POST['theme'] as $theme) {

$search_theme = "AND keywords LIKE '%*$theme*%' ";

}

} else {

$search_theme = " ";

}

 

if ($geography) {

foreach($_POST['geography'] as $geography) {

$search_geography = "AND geography = '$geography' ";

}

} else {

$search_geography = " ";

}

 

$search_query = "SELECT * FROM library WHERE (title_en LIKE '%$searchterm%' OR  fulltext_en LIKE '%$searchterm%') $search_type $search_year $search_theme $search_geography ORDER BY dateofissue DESC, type DESC, title_en DESC";

 

 

The original values come from a form which one fills in. So geography[] is parsed to this and it can have one or more values. For example, if geography[]=africa and geography[]=asia, the query will only show "AND geography=asia" as the latter comes later.

 

However the thing would work if I put the variable in the URL, like thisfile.php?geography[]=africa&geography[]=asia

 

Can someone help me?

 

Link to comment
Share on other sites

you have to separate the geography values from the name. right now it is looking for geography to = all the values

 

if($geography){
$sg = implode(", ", $_POST['geography']);
$search_geography = "AND geography IN ('$sg') ";
}

 

You can simplify the others the same way if you are passing each POST as an array.

 

Ray

 

Link to comment
Share on other sites

Or you can use concatenation. The reason your variables are being overwritten is because every loop you're tell it to set the variable to the current value, not add on.

 

change your strings to be:

 

$search_geography.= "AND geography = '$geography' ";

 

That .= means "take the current $search_geography variable and add this (everything after the .=) to the end of it"

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.