mercredi 25 février 2015

Advanced search using php/mysql?

currently I am working on an online shop using PHP and Mysql.


this online shop will have an advanced search function.


the search function will include stuff like SIZE and COLOR.


I store the products in the products table in mysql database which looks like this:



id product_name price category
3 shirt 29 clothes
4 shoes 19 clothes


and I have two separate tables in mysql database for COLORS and SIZES and I use the for each loop to insert the data into those two tables for each selected size/color!


so they look like this:


color's table:



idc id product_name color
1 3 shirt red
2 3 shirt white
3 3 shirt black
4 3 shirt pink
5 4 shoes black
6 4 shoes brown


sizes table:



idc id product_name size
1 3 shirt small
2 3 shirt large
3 4 shoes 5
4 4 shoes 6


the column id is identical in all the tables.


now, I have some simple checkboxes in my php search page which i want to allow users to click on search by size/color etc etc.


the check boxes look like this:



<input type="checkbox" name"size[]" value"small"/>
<input type="checkbox" name"size[]" value"large"/>
<input type="checkbox" name"size[]" value"xlarge"/>
<input type="checkbox" name"size[]" value"xxlarge"/>
<input type="checkbox" name"size[]" value"6"/>
<input type="checkbox" name"size[]" value"7"/>
<input type="checkbox" name"size[]" value"8"/>
<input type="checkbox" name"size[]" value"9"/>

<input type="checkbox" name"colors[]" value"red"/>
<input type="checkbox" name"colors[]" value"white"/>
<input type="checkbox" name"colors[]" value"black"/>
<input type="checkbox" name"colors[]" value"brown"/>


the issue that I have is that I cannot come up with any solution (code) for this advanced search!


question that I have:


1- am I approaching this correctly by separating size, colors and products in different tables in mysql database?


2- if the answer to the above is yes, is using the following code correct:



SELECT
products.id,
products.product_name,
products.price,
products.category,
FROM products
INNER JOIN colors ON products.id= colors.id
INNER JOIN sizes ON products.id= sizes.id
WHERE (
sizes.size LIKE '%whatever%' OR
colors.color LIKE '%whatever%'
)


any help and advise would be greatly appreciated.


Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire