Hi all, I am kinda new to web design and also PHP. I am currently working on a Mobile Tyre Fitting website for my dad and need a little help on a search form.

The search form has 3 dropdown menu's which are WIDTH , PROFILE and SIZE. I have a database with these in a table as well as some other information. Using dreamweaver I can create a record sheet to filter only one of these fields and get some results, but I need the user to be able to select the exact tyre size they want i.e: 245/45/19 and then only tyre sizes in the database matching that tyre size will be display on the search_results page.

Does anyone have a idea how I can do this? If you could explain it in simple terms I would be grateful.

Hope you can help!

Regards

Kieron

Recommended Answers

All 6 Replies

This is just demo code.
Change your database table field names and run it.

<?
	if(isset($_POST['search']))
	{
		echo 'Search Result :<br />';
		$width = mysql_escape_string($_POST['width']);
		$profile = mysql_escape_string($_POST['profile']);
		$size = mysql_escape_string($_POST['size']);
		
		$where = " 1=1 ";
		if($width != "")
			$where.=" AND width='$width'";
		if($profile != "")
			$where.=" AND profile='$profile'";
		if($size != "")
			$where.=" AND size='$size'";
			
		$sql = "select * from mobile where $where";
		$rs = mysql_query($sql);
		while($sar = mysql_fetch_assoc($rs))
		{
			echo '<br />'.$sar['mobile_name'];
		}
	}
?>
<form name="frm" method="post">
WIDTH :
<input name="width" type="text" />
PROFILE :
<input name="profile" type="text" />
SIZE :
<input name="size" type="text" />
<input name="search" value="Search" type="submit" />
</form>

Hope this helps.

Hi thanks very much for your quick reply. This kinda helps me out, just that I need the text fields to be drop down menu's where the user selects the right size. How would I get that to work?

Many Thanks again

Kieron Thomas

here we go...

<?
	if(isset($_POST['search']))
	{
		echo 'Search Result :<br />';
		$width = mysql_escape_string($_POST['width']);
		$profile = mysql_escape_string($_POST['profile']);
		$size = mysql_escape_string($_POST['size']);
		
		$where = " 1=1 ";
		if($width != "")
			$where.=" AND width='$width'";
		if($profile != "")
			$where.=" AND profile='$profile'";
		if($size != "")
			$where.=" AND size='$size'";
			
		$sql = "select * from mobile where $where";
		$rs = mysql_query($sql);
		while($sar = mysql_fetch_assoc($rs))
		{
			echo '<br />'.$sar['mobile_name'];
		}
	}
?>
<form name="frm" method="post">
WIDTH :
<select name="width">
<option value="">- - Select Width - -</option>
<option value="200">200</option>
<option value="250">250</option>
<option value="300">300</option>
</select>
PROFILE :
<select name="profile">
<option value="">- - Select Profile - -</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="150">150</option>
</select>
SIZE :
<select name="size">
<option value="">- - Select Size - -</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
</select>
<input name="search" value="Search" type="submit" />
</form>

You can change drop down values as per your need.

I may be off-track, but are you meaning that you want the drop-down values to populate as you go?

Ie, you select a value from "Width" and based on that selection you are offered options for "Profile" and then offered options for "Size" based on the two previous selections.

Do I have that right? And are you trying to this all on one page or as a multiple step (using different pages for each drop-down offering) interface?

HI TySyBy, thanks for your message. Yes that would be great idea and I have seen that kinda search done on another tyre website. However I just want to start simple at the minute for a 'simple mind'.

I have a database with the following fields in it; WIDTH, PROFILE and SIZE. I have created a form with 3 drop down lists named as WIDTH, PROFILE and SIZE. All I need is the user to be able to select from the 3 drop down boxes and when they click the search button, it brings up only the tyres with that size from the database. I.e: WIDTH = 245 PROFILE = 45 SIZE = 18.

Another thing is, I am trying to do this is Dreamweaver CS5 using the 'Advanced Recordset' feature, so I need to know how to do it using the dreamweaver features and not just copying and pasting code! Would I have to do something with the 'Variables' section??

Hope you can help! I just don't know enough yet in the code region!!

Regards

Kieron

I'll admit, I don't know much about using Dreamweaver for stuff like that (and I have CS4), so I don't know how Advanced Recordset works.

However, I've found that Dreamweaver lacks in the PHP department (I only ever use it for HTML when I'm too lazy to do it by hand), in my opinion. Again, I don't have CS5, so maybe it's better, but if I were to make a suggestion, it would be to skip learning the Dreamweaver-specifics and make it easier on yourself by learning the MySQL functions for PHP. Sometimes I think Dreamweaver over-complicates things (again, just a personal observation).

Maybe try out NetBeans IDE with PHP so you can get the code suggestion and syntax highlighting, which I found made things much easier when I was learning PHP.

But back to your original question, I think something like what vibhadevit posted would be good to use. Since you're new to PHP, here's the code tweaked a little so you can see what it's doing step-by-step and with comments for clarification:

<?
if (isset($_POST['search'])) { //If the form's "Submit" button was clicked...
    echo 'Search Result :<br />';

    //Set variables to info posted from the form
    $width = $_POST['width'];
    $profile = $_POST['profile'];
    $size = $_POST['size'];

    //Escape the strings so they're safe to use in a MySQL query
    $width = mysql_escape_string($width);
    $profile = mysql_escape_string($profile);
    $size = mysql_escape_string($size);

    //Start building the query
    $where = " 1=1 ";

    if ($width != "") {  //If the user selected a width
        $where.=" AND width='$width'";  //Add a width specification to the query
    }

    if ($profile != "") {  //If the user selected a profile
        $where.=" AND profile='$profile'"; //Add a profile specification to the query
    }

    if ($size != "") { //If the user selected a size
        $where.=" AND size='$size'"; //Add a size specification to the query
    }

    //If the user made all three selections, your query (essentially) would be like this example:
    //$sql = "select * from mobile where 1=1 AND width='$width' AND profile='$profile' AND size='$size'";
    $sql = "select * from mobile where $where";
    $rs = mysql_query($sql);
    while ($sar = mysql_fetch_assoc($rs)) {  //For each 'mobile_name' in the database that matches the query
        echo '<br />' . $sar['mobile_name']; //Print a new line along with the value from 'mobile_name' field
    }
}
?>
<form name="frm" method="post">
    WIDTH :
    <select name="width">
        <option value="">- - Select Width - -</option>
        <option value="200">200</option>
        <option value="250">250</option>
        <option value="300">300</option>
    </select>
    PROFILE :
    <select name="profile">
        <option value="">- - Select Profile - -</option>
        <option value="50">50</option>
        <option value="100">100</option>
        <option value="150">150</option>
    </select>
    SIZE :
    <select name="size">
        <option value="">- - Select Size - -</option>
        <option value="10">10</option>
        <option value="20">20</option>
        <option value="30">30</option>
    </select>
    <input name="search" value="Search" type="submit" />
</form>
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.