Jump to content

A Slightly Complicated MySQL Query


anthonydamasco

Recommended Posts

Hello All,

 

I'm writing an advanced search results page for a website. It's pretty simple except the front end team decided to put a lot of check boxes on the posting page.

 

 

(I'll simplify everything to make the situation ez to understand)

 

So I am constructing a query to pull information from the columns

 

collection

construction

color_e

match

 

and I have variables

$collection

$construction

$red

$green

$blue

$yellow

$match

 

 

Alright so the query needs to be something like

 

Select * From products Where Collection LIKE '$collection' AND construction Like '$construction' AND (color_e like '$red' OR color_e like '$green' OR color_e like '$blue') And match like '$match'

 

The part im having trouble with is the "(color_e like '$red' OR color_e like '$green' OR color_e like '$blue')" part, i want to pull results from any row that has the colors equal to what is being requested. they colors are check boxes so if they are unchecked they equal "XXXXXXX" and if they are checked they equal the color value assigned.

 

Any help?

Link to comment
Share on other sites

Try somthing like this hope it works but im not sure it sill :)

 

Select * From products

 

Where

 

Collection LIKE '$collection'

 

AND construction Like '$construction'

 

AND color_e like '$red' OR '$green' OR '$blue'

Link to comment
Share on other sites

Well, if you were to set up your checkboxes like:

 

Blue: <input type="checkbox" name="colours[]" value="Blue" /><br />
Red: <input type="checkbox" name="colours[]" value="Red" /><br />
//etc

 

You could then do something like:

 

<?php
$colour = "'".join("','",$colours)."'";
$sql = "SELECT * FROM products WHERE Collection LIKE '$collection' AND color_e IN ($colours) AND...."
?>

Link to comment
Share on other sites

thanks guys I just have one more question.

 

I have an dropdown

 

<select name="collection">

<option value="" selected="selected">All Collections</option>

<option value="Altair">Altair</option>

<option value="Alto">Alto</option>

</select>

--

$collection = $_post["collection"];

--

SELECT * FROM products WHERE collection = '%$collection%'

 

if someone chooses "All collections" i want all collections to display. what value should be set under "all collections"

 

Link to comment
Share on other sites

Alright, I'm trying this method,

 

how do i post this to create the "$colours" variable

 

Well, if you were to set up your checkboxes like:

 

Blue: <input type="checkbox" name="colours[]" value="Blue" /><br />
Red: <input type="checkbox" name="colours[]" value="Red" /><br />
//etc

 

You could then do something like:

 

<?php
$colour = "'".join("','",$colours)."'";
$sql = "SELECT * FROM products WHERE Collection LIKE '$collection' AND color_e IN ($colours) AND...."
?>

Link to comment
Share on other sites

Set the value for all collections as "%" that is Mysql wildcard.

 

<?php
$colour = $_POST['colours'];
$colours = "'".join("','",$colour)."'";
$sql = "SELECT * FROM products WHERE Collection LIKE '$collection' AND color_e IN ($colours) AND...."
?>

 

Ray

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.