Hi there,

I am looking for help to code a php mysql search form with 4 dropdown list. I want the user to search for accommodation establishments by using a citytown, tourism route, accommodation profile and accommodation tariffs. All this will be in the dropdown list which reflects the fields in the database table users. I can connect to the database but cannot get my head around how to code the php script to get results for all fopur fields from the database. Also if the users only select one, two or three of the dropdown lists how do I code that the specific result is only returned in the search.

Woiuld appreciate if anyone can point me in the right direction for me to strat learning. herewith my code so far:

<?php
include 'dbc.php';
include 'classes/selectbox.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="" method="post" name="myform" id="myform">

<h2>Search Accommodation:</h2> 
        <table width="90%" border="0" align="center" cellpadding="3" cellspacing="3" class="forms">

        <tr>
                <td>CityTown<span class="required">*</span><br />
                    <?php
        $mybox = new SelectionBox();
//This is the name of the selection box
$mybox->SetName("citytown");
//This is how to set an option and its value
$mybox->SetOption("Jeffreys Bay","Jeffreys Bay");
$mybox->SetOption("St Francis Bay","St Francis Bay");
$mybox->SetOption("Port Elizabeth","Port Elizabeth");
$mybox->SetOption("Cape St Francis","Cape St Francis");
$mybox->SetOption("Humansdorp","Humansdorp");
$mybox->SetOption("Oyster Bay","Oyster Bay");
//Finally, we output the box and specify which option value we want selected
$mybox->CreateBox("citytown");
?></td></tr>
<tr>
              <td>Tourism Route<span class="required">*</span><br />
                    <?php
        $mybox = new SelectionBox();
//This is the name of the selection box
$mybox->SetName("tourism_route");
//This is how to set an option and its value
$mybox->SetOption("Kouga Tourism Route","Kouga Tourism Route");
$mybox->SetOption("Tsitsikamma Tourism Route","Tsitsikamma Tourism Route");
$mybox->SetOption("Sunshine Coast","Sunshine Coast");
$mybox->SetOption("Greater Addo Route","Greater Addo Route");
//Finally, we output the box and specify which option value we want selected
$mybox->CreateBox("tourism_route");
?>                </td>
              </tr>
<tr>
                <td>Acc Category<span class="required">*</span><br />
                    <?php
        $mybox = new SelectionBox();
//This is the name of the selection box
$mybox->SetName("acc_category");
//This is how to set an option and its value
$mybox->SetOption("Bed & Breakfast","Bed & Breakfast");
$mybox->SetOption("Guest House","Guest House");
$mybox->SetOption("Selfcatering","Selfcatering");
$mybox->SetOption("Hotel","Hotel");
$mybox->SetOption("Lodge","Lodge");
$mybox->SetOption("Backpacker","Backpacker");
//Finally, we output the box and specify which option value we want selected
$mybox->CreateBox("acc_category");
?></td></tr>

             <tr>
              <td>Accommodation Tariffs<span class="required">*</span><br />
                    <?php
        $mybox = new SelectionBox();
//This is the name of the selection box
$mybox->SetName("acc_tariffs");
//This is how to set an option and its value
$mybox->SetOption("R0 - R350.00","R0 - R350.00");
$mybox->SetOption("R350.00 - R550.00","R350.00 - R550.00");
$mybox->SetOption("R550.00 - R850.00","R550.00 - R850.00");
$mybox->SetOption("R850.00 - R1 000.00","R850.00 - R1 000.00");
//Finally, we output the box and specify which option value we want selected
$mybox->CreateBox("acc_tariffs");
?></td></tr>

    <tr>

    <td>

    <input name="submit" type="submit" value="Search"/>
    </p> 
    </td>

    </tr>          
</table>
</form>

</body>
</html>

Also herewith the php class for the dropdown lists I am using:

<?php

/**
 SelectionBox class
 */
class SelectionBox
{
function SetName($name)
{
    $this->myname = $name;
    }
    function SetOption($optionvalue,$optionname)
    {
        $this->options[$optionname]=$optionvalue;
        }
        function CreateBox($selected)
        {
        if($this->options)
        {
            print "<SELECT name='".$this->myname."'>\n";
            //Create Options
            foreach($this->options as $optionname=>$optionvalue)
            {
                print "<OPTION value ='$optionvalue'";
                //Check if option should be selected
                if($selected == $optionvalue)
                {
                    print "SELECTED";
                }
                print ">$optionname</OPTION>\n";
            }
            print "</SELECT>";
        }
        else
        {
            print "Error: No options specified";
        }
    }
}
/*
$mybox = new SelectionBox();
//This is the name of the selection box
$mybox->SetName("MySelectionBox");
//This is how to set an option and its value
$mybox->SetOption("EU","Europe");
$mybox->SetOption("IN","India");
$mybox->SetOption("MX","Mexico");
//Finally, we output the box and specify which option value we want selected
$mybox->CreateBox("MX");
*/
?>
Member Avatar for diafol

This isn't a complete solution, but should give you an idea:

<?php
//CHECK FOR FORM BEING SENT
if(!empty($_POST)){
    //CLEAN INPUTS
    $post = array_map('intval',$_POST); //usually use mysql_real_escape_string but you're only accepting integers

    //GET INPUT VALUES - IGNORE IF = 0 OR FALSE
    if($post['citytown']){  
        $where[] = "`citytown` = {$post['citytown']}";  
    }
    if($post['tour']){  
        $where[] = "`tour` = {$post['tour']}";  
    }
    if($post['profile']){   
        $where[] = "`profile` = {$post['profile']}";    
    }
    if($post['tariff']){    
        $where[] = "`tariff` = {$post['tariff']}";  
    }

    //MAKE A WHERE CLAUSE STRING FROM $where ARRAY
    $where_clause = (isset($where)) ? implode(", ",$where) : '';

    //RUN QUERY
    $result = "SELECT * FROM table $where_clause";
    //...(display in the usual way)...
}

?>


<form method="post">

    <select name="citytown">
        <option>(Choose a town)</option>
        <option value="2">Aberystwyth</option>
        <option value="5">Brynaman</option>
        <option value="9">Cwmllynfell</option>
        <option value="3">Tycroes</option>
    </select>

    <select name="tour">
        <option>(Choose a route)</option>
        <option value="1">Brecon Beacons</option>
        <option value="2">Pembrokeshire Coast</option>
        <option value="3">Snowdonia</option>
    </select>

    <select name="profile">
        <option>(Choose an accommodation)</option>
        <option value="1">Shack</option>
        <option value="10">Teepee</option>
        <option value="9">Tent</option>
        <option value="5">Winnabago</option>
    </select>

    <select name="tariff">
        <option>(Choose a tariff)</option>
        <option value="1">£20 - 40</option>
        <option value="2">£41 - 60</option>
        <option value="3">£61 - 80</option>
        <option value="4">£81 - 100</option>
        <option value="5">£ >100</option>
    </select>
    <input value="Search" name="sub" type="submit" />
</form>

Hey Ardav,
Thanks for the help. I will implement your suggestion and see how far I get.
Regards
Steven

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.