Jump to content

[SOLVED] Joining 2 select statements depending on condition


meames

Recommended Posts

Hi all,

Not sure if this fits in the PHP or MySQL section, I guess its more PHP though so here goes......I am currently trying to write a search script which takes a search term from the user and also takes 3 options on what to search for depending on what the user wants:

  • Keywords
  • Story Title
  • Main Story

 

I have passed all of this to a new page which has the following query in:

 

$Query = "SELECT * FROM News WHERE Park LIKE '%$SearchTerm%'";

 

The 'Park' will be searched every time, but I only want to search on the other options if the user has selected them, I thought about adding the following:

 

if(!empty($Keywords)) {
$Query = $Query + "OR Keywords LIKE '%$SearchTerm%'";
}

 

and so on for the other 2 options, yet this does not work. There must be some way of doing this, can anybody help?

 

Thanks.

Thanks, echo'ing out the query led went to show me what was going off. I have now got it working with the following code:

 

$Query = "SELECT * FROM News WHERE Park LIKE '%$EditedSearchTerm%'";

if(!empty($Keywords)) {
$Query = "$Query OR Keywords LIKE '%$EditedSearchTerm%'";
}


 

Really happy over this, I can move on now :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.