Skip to main content
Participant
November 6, 2009
Question

PHP link where one option includes 3 values from the same table column

  • November 6, 2009
  • 4 replies
  • 862 views

What I'm trying to do is set up a series of menu items for categories of merchandise (rugs), with subcategories for size. I've succeeded in making links where the user can choose ALL sizes within a category (by not specifying size as a parameter at all) and I've succeeded in making links where the user can choose a specific size within a specific category, but I'm not able to create links where the user can choose a specific size OR see ALL sizes.

Here's what I've got:

3 tables: products, categories & size
The size table has 2 columns - sizeName, and SizeID - the sizeID column is an integer and it links to products table
The categories table also has 2 columns - categoryName and categoryID - the categoryID column is also an integer which links with the product table

A menu with categories, and each category has a drop down menu where the viewer can choose a size.
Each size link has a parameter which includes the categoryID as well (in following example, all the sizes are within one category):
small - products.php?categoryID=8&sizeID=1
medium - products.php?categoryID=8&sizeID=2
large - products.php?categoryID=8&sizeID=3


On the receiving page (products.php) I created a recordset which looks like this (I'm looking at the recordset dialog box within Dreamweaver CS4):

SELECT price, `description`, categoryID, largeImage, smallImage, sizeID
FROM products
WHERE categoryID = colname AND sizeID = varSize
ORDER BY price ASC

the variables look like this:

Name: colname
type: Integer
default value: -1
Runtime value: $_GET['categoryID'}

Name: varSize
Type: Integer
Default value: 1
Runtime value: $_GET['sizeID']

(Dreamweaver created colname and I created varSize. I'm not really sure why the default values are as they are).

This works. But how do I create a link parameter for all three sizes? And how do I edit my recordset so it has a variable with all 3 sizes and how does that variable get written into the WHERE part of the code? Or, am I making this harder than it should be?

Any help would be greatly appreciated.

Thanks,
Dave

This topic is closed to new replies. Start a new post to keep the conversation going.

4 replies

Participating Frequently
November 6, 2009

Create a menu item "All Sizes". In your php script, test the value of the menu and if the selected item is "All Sizes", then don't include size in your where clause:

SELECT price, `description`, categoryID, largeImage, smallImage, sizeID
FROM products
WHERE categoryID = colname
ORDER BY price ASC

Participant
November 6, 2009

I think I get what you mean - but I want to have both (an "all" option, or particular sizes) going to the same page. Is there a way to have a SELECT like the one I had before (specifying sizes) and a second part of the code that says basically "OR, if no size specified in URL"  followed by what you set up:

SELECT price, `description`, categoryID, largeImage, smallImage, sizeID
FROM products
WHERE categoryID = colname
ORDER BY price ASC

How would that be written?

Thanks,
Dave
Participating Frequently
November 6, 2009

>I think I get what you mean - but I want to have both

>(an "all" option, or particular sizes) going to the same page.

Of course.

>Is there a way to have a SELECT like the one I had before (specifying sizes)

>and a second part of the code that says basically "OR, if no size specified in

>URL"  followed by what you set up:

The easiest way is to just have 2 different select statements. Test the condition of the size field and branch to the appropriate statement. This will work well for your example where you only have 2 possible select statements - one that includes size and one that does not. This technique can get messy if you are testing more conditions. In that case, it usually works better to dynamically construct the where clause string, testing the condition and appending the string as appropriate.

In your case, I would go with option A.