Jump to content

if statement alternative?


spangle1187

Recommended Posts

I have four check boxes that will determine how a search is conducted and I have retrieved the data through _POST[]

 

The problem I have is how can I act upon the data returned? Each of the four text boxes could independently start a search but if I use an If statement the first choice will block the rest of the options eg.

if ($clientValue == "on"){
echo "perform a search based on above";
}

 

 

Link to comment
Share on other sites

The user fills in a search form containing four input boxes and four check boxes. The four check boxes are used to specify which elements the user wants to use to query the db:

 

1. client name

2. Room

3. Subject

4. Date

 

The start search button attached to the from calls run_search.php and the information is retrieved using $_POST.

 

Each of the four above can be used as a combination or as an independent search, my first though was to use an if statement based on the check box values:

 

if ($clientValue == "on"){
echo "perform a search based on above";
}

 

but the problem is if the first check box is ticked the the if statement will only run the first part of the statement as the condition has been met.

 

What can I use to search through the results to pick out the combination of the search?

 

I have tried using an array but not sure if this is right?

	$box=$_POST['box'];

while (list ($key,$val) = @each ($box)) {
echo "$val,";
}

 

The results will then be passed into an sql query and replace the variable $input and $string to complete the query.

 

$input = "Sam";
$string = "clientName == ".'"'.$input.'"';
$table_id = 'booking'; 	
$query ="SELECT * 
FROM booking
WHERE ". $string;

//$test = mysql_query($query);	
echo $query;

 

I hope this explains my problem a little better, thanks

 

[attachment deleted by admin]

Link to comment
Share on other sites

You say you have four text boxes and four checkboxes. Do the checkboxes correspond to the textboxes? If so, why do you need the checkboxes? Just use the fact that a value has been entered into a search field.

 

Anyway, the solution to your problem is fairly simple. Use the input to "build up" the query. Then run the query after you have checked all the user input. Example:

$whereClauses = array();
if(isset($_POST['checkbox1']))
{
    $whereClauses[] = "field1 LIKE '%{$_POST['textbox1']}%'";
}
if(isset($_POST['checkbox2']))
{
    $whereClauses[] = "field2 LIKE '%{$_POST['textbox2']}%'";
}
if(isset($_POST['checkbox3']))
{
    $whereClauses[] = "field3 LIKE '%{$_POST['textbox3']}%'";
}
if(isset($_POST['checkbox4']))
{
    $whereClauses[] = "field4 LIKE '%{$_POST['textbox4']}%'";
}

$searchQuery = "SELECT * FROM tableName WHERE " . implode(' AND ', $whereClauses);

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.