Jump to content

[SOLVED] PHP switch function help


hannah415

Recommended Posts

Hi, I am trying to create a search function for a directory website. On the search function on html page I have a dropdown menu. From this you can select one OF 3 options - shoes, handbags, shoes and handbags. I have a php process form page where it takes this infor before runninng a query in the sql database.

 

My question is that I would like the one option to have multiple values - when user clicks shoes, the query will search for a field containing shoes (this bit is fine) but also I want it to select another field to search for a value - this field will depend upon the what option the user picked in the question. The problem being, that unless i rename the fields in the table from lets say, Sbrand to Shoes and Hbrand to handbags i'm not sure  how to action

 

What I need is that if value on option was shoe then I need it linked to pick from Sbrand Now I think that I can use a switch function but the current issue is that I must have it in the wrong place as when I tried it as per below code, it appears that the search is looking for a field called the option value so the switch is not working. Perhaps I need to move the switch function from the php process page to the html page but I’m not sure

 

Any help much appreciated

 

HTML code………… this works on a method=”get”

 

<select name="type">

<option value="none">-- Please Select --</option>

<option value="Shoes">Shoes</option>

<option value="Handbags">Handbags</option>

<option value="Shoes and Handbags">Shoes and Handbags</option>

</select>

 

 

 

Here’s the PHP code

 

$search_type=$db->escapeString($_GET['type']);

switch ($type){

case "Shoes":

echo "Sbrand";

break;

case "Handbags":

echo "Hbrand";

break;

case "Shoes and Handbags":

echo "SHbrand";

break;

}

 

 

 

$result=$db->query("SELECT Sbrand, Hbrand, SHbrand, SB, Retailer, Address, Town, County, Postcode, Postcode_code, Phone, Website FROM database WHERE SB LIKE 'type' AND $search_type LIKE '%$searchterm%'");

 

 

 

 

Any help much appreciated as i am new to this

 

 

Link to comment
Share on other sites

Have you noticed you are using all different variable names here?

 

$search_type

$type

$searchterm

 

$type isn't being set by anything (unless register_globals is on)

 

for a switch you can do more than echo out something fex;

$myVariable = cleanFunction($_GET(variable);  //where cleanFunction sanitizes the imput

switch ($myVariable){
case "Type 1": 
$columnName = "TypicalOne";
echo "Choosing TypicalOne";
break;
case "Type 2": 
$columnName = "TypicalTwo";
echo "Choosing TypicalTwo";
break;
default: $columnName = "Typicals";
echo "Choosing Default";
}

 

So use the switch to set the variable that changes in the query.

 

Link to comment
Share on other sites

thanks for that - I think the names are ok - I have left quite a bit of coding out so i think the actual names are ok but i will double check - it appears to be ok as the query will run but it says it can't find the word in field 'Shoes' because there is no field (this is where it should switch to SBrand)

 

I will try to change the code - maybe what you suggest to change escapeString to cleanFunction will solve the issue

 

Link to comment
Share on other sites

you may benefit from replacing the select box with radio buttons (you woul donly need 2 for your current functionality - adding a third option would mean 6 options in a select drop down but still just 3 for check boxes...)

 

trick here is to use bitwise operators (using three options to demonstrate)...

 

search options

<?php
define ('SEARCH_SHOE' , 1);
define ('SEARCH_BAG' , 2);
define ('SEARCH_HAT' , 4);

....
?>

 

markup

<form method="get" ...>
<fieldset>
<legend>Directory Search</legend>
<label for="searchterm">Find:</label>
<input type="text" name="searchterm" />
<div class="search-options">
  <label><input type="checkbox" name="searchoption[]" value="<?php echo SEARCH_SHOE; ?>" /> Shoes</label>
  <label><input type="checkbox" name="searchoption[]" value="<?php echo SEARCH_BAG; ?>" /> Bags</label>
  <label><input type="checkbox" name="searchoption[]" value="<?php echo SEARCH_HAT; ?>" /> Hats</label>
</div>
<input type="submit" name="submitbttn" value="Go" />
</fieldset>
</form>

 

search code

 

<?php
function addTerm (&$term,$key)
{
$term .= "LIKE '%" . $_GET['searchterm'] . "%'";
}

$search = array_sum ( $_GET['searchoption'] );

$fields = array();

if ( $seach & SEARCH_SHOE )
{
$fields[] = '`Sbrand`';
}
if ($search & SEARCH_BAG )
{
$fields[] = '`Bbrand`';
}
if ( $search & SEARCH_HAT )
{
$fields[] = '`Hbrand`';
}

array_walk($field,'addTerm');

$qry = "SELECT * FROM `directory_table` WHERE " . implode(' OR ' , $field);
echo $qry; // for testing - you can copy and paste to check query runs OK.
$result = mysql_query($qry);
?>

 

Hopefully that code will be a little more robust and maintainable for you...

Link to comment
Share on other sites

I see what you both mean now about the names - there are actually a few other fields on the search and I have only posted a few.

 

To start with the dropdown option name is 'type and 'searchterm' is another textbox for putting the name of the brand we are searching for.

 

Here is the HTML code - I will try and post the php code when I get home this evening as it will make it easier to see

 

 

<form action="processform.php"  method="get" target="results.html">

<fieldset>

<label for="type">Search for:</label><br />

<select name="type">

<option value="none">-- Please Select --</option>

<option value="Shoes">Shoes</option>

<option value="Handbags">Handbags</option>

<option value="Shoes and Handbags">Shoes and Handbags</option>

</select> <br />

 

 

  <label for="searchterm">

  Brand:

  </label><br />

  <input type="text" id="searchterm" name="searchterm" /><br />

 

  <table>

 

<tr><td><input type="hidden" name="on0" value="Online or Highstreet"/>Online or Highstreet?</td></tr><tr><td><select name="os0" onChange="message2(this.form,'textbox')"/>

 

    <option value="none">-- Please Select --</option>

 

    <option value="online">Online</option>

 

    <option value="highstreet">Highstreet</option></td></tr>

 

 

 

<tr>

 

          <td>

 

            <input type="hidden" name="on1"/>Postcode:</td>

 

        </tr>

 

        <tr>

 

          <td>

 

          <textarea name="postcode" cols="11" rows="1"  id="textbox" style="visibility:hidden"></textarea></td>

 

        </tr>

 

</table>

<br /> <br />

<input type="hidden" name="searching" value="yes" />

<input type="submit" name="search" value="Search" />

</fieldset>

</form>

 

 

 

 

 

ToonMariner - thanks for your answer - I understand what you say about using checkboxes but the problem is i actually need it to have two different values as I need it to keep the other values under name 'type' so that i can run this query

 

FROM database WHERE SB LIKE 'type' AND

Link to comment
Share on other sites

That's why I suggested check boxes NOT radio buttons.

 

If you go back and look at the code I posted - it allows you to select as many options as you wish and you can alter the string glue on implode to ' AND ' if you only want results where the search term appears in all the fields specified.

 

Have a look at bitwise operators and you will see how those if statements allow all search options to be included.

 

Try the code and echo out the query - you should be pleasantly surprised.

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.