...

View Full Version : question on coding



harkly
06-30-2010, 04:44 PM
I have a search form where some of the info does not have to be selected and I am a bit puzzled as to how to code my second page when those options are not used.

Can I set up separate $query's based on the blank input?


if (empty($search1))
$query = search

else if (empty($search2))
$query = search2

else
$query = search3

if ($result = mysql_query($query))

or do I need to do the full code in each if statement?

Any suggestions would be greatly appreciated!!

tomws
06-30-2010, 05:38 PM
Of course you can. Think about it. What are you attempting to do in the if/else tree? Assign a string to a variable. There's nothing to restrict you from doing that.

I'll bet you could have found the answer through testing in the time it took to write the post and get an answer.

harkly
06-30-2010, 05:39 PM
Actually I did!

But since I am new I want to make sure that I am not just pulling some of the logic out of thin air! :D

tomws
06-30-2010, 05:47 PM
Excellent! I'm surprised at the number of people who actually don't try, but wait for spoon-feeding.

So, here's a bonus. You'll see code like this in some apps:

// ...
$query = "SELECT `foo`, `bar` FROM `baz` WHERE";
if (!empty($search1))
{
$query .= " `foo`='$search1'";
}
elseif (!empty($search2))
{
$query .= " `bar`='$search2'";
}
else
{
$query .= " `qux`='$search3'";
}
if ($result = mysql_query($query))
{
// ...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum