Reputation: 29
I have 3 table in a database and I would like to populate two dropdown menu with value from 'brands and categories' table, so that a user can select categories and brands and then a search or a display page would be produced on the basis of their selections from products table
I am fairly new to php, but have a MySQL database to which I can add/edit/del and produce results from, but am unsure how to do the above..
I have 3 tables
brands:
brand_id
brand_title
categories:
cat_id
cat_title
products:
product_id
product_cat
product_brand
product_price
product_keywords
my php code here: This code fetch data from brands and categories table in 2 dropdown. now i want to search by keywords and display data from product table by what options selected from brands and categories table. please take a look this link: http://ruqayyahfashion.com/product/1.php
<?Php
mysql_connect("localhost", "root", "") or die("Connection Failed");
mysql_select_db("ecomerce")or die("Connection Failed");
$sql="SELECT cat_id, cat_title FROM categories";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result))
{
$cat_id=$row["cat_id"];
$cat_title=$row["cat_title"];
$options.="<OPTION VALUE=\"$cat_id\">".$cat_title;
}
$sql1="SELECT brand_id, brand_title FROM brands";
$result1=mysql_query($sql1);
$options1="";
while ($row1=mysql_fetch_array($result1))
{
$brand_id=$row1["brand_id"];
$brand_title=$row1["brand_title"];
$options1.="<OPTION VALUE=\"$brand_id\">".$brand_title;
}
?>
<form >
<input type="text" class="form-control" placeholder="Search a Product">
<SELECT NAME=Categories>
<OPTION VALUE=0>Select a Category
<?=$options?>
</SELECT>
<SELECT NAME=Brands>
<OPTION VALUE=0>Select a Brand
<?=$options1?>
</SELECT>
<button type="submit" name="btn">Search</button>
</form>
Upvotes: 0
Views: 851
Reputation: 701
I think your code is correct, only you've forgot to close the OPTION-tags
$options.="<OPTION VALUE=\"$cat_id\">".$cat_title."</OPTION>";
$options1.="<OPTION VALUE=\"$brand_id\">".$brand_title."</OPTION>";
Also make it in HTML code for empty Select options
<OPTION VALUE="0">Select a Brand</OPTION>
To get a results you'll need to create another php - for an example dowork.php where you'll make your query on base of the costumer selection. You'll receive costumer choice in $_POST['Brand'] and $_POST['Categories']. But to got this you need first modify your HTML:
Upvotes: 0