Forum Moderators: coopster & phranque

Message Too Old, No Replies

First dynamic site

Advice on product database needed.

         

Birdman

2:47 pm on Oct 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I think this will be my new home for a while. I've decided to make my current website project dynamic. So far, I've got my nav menu dynamic so I won't have to edit multiple pages when a change is requested by the client.

Now, I'm starting to set up a product database. I could use some advice on the best way to set it up. I'm unsure about one field in particular. The "categories" field. Can this field have multiple values? Many item in the catalog will appear in multiple categories.

Here is the field list that I've come up with so far:
item_no
cats
weight
in_stock
cost
name
desc
included

I would appreciate any advice on how to best lay this out so I don't have to change everything after I realize I laid it out wrong.

Thanks,
Birdman

Dreamquick

2:56 pm on Oct 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could the categories field several ways;

1) text-based containing multiple keywords so you could just do multiple likes to return matching results.
2) separate table linking a product to the categories it occupies so you could just return all product codes (and by extension products) based on a set of categories
3) multiple fields each representing a keyword, you could query this using simple logic

3 is the easiest but the hardest to upgrade, 1 is simple but lazy and 2 is a little more complex but will yield the best results in the long term as its easy to update/expand and fits with the relational model.

At the moment I'd say what you have seems reasonable enough as you aren't duplicating information and there is really no better way to express the data in a pure relational format.

- Tony

Birdman

3:25 pm on Oct 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, if I use a separate table(categories), it would be something like this:

table "categories"
item_no
category_1
category_2
category_3
category_4
category_5

And set the values to "yes" or "no" depending on where you want the item to appear?

Thanks for the reply, dreamquick

ukgimp

3:44 pm on Oct 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



G'Day Birdman

Although written in ASP the following has a tutorial on the kind of things you need for a ecommerce database

www.princeton.edu/~rcurtis/ultradev/ecommdatabase.html

Here is a tutorial on php basics
www.webmasterbase.com/article.php/530

Cheers

Dreamquick

3:50 pm on Oct 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not quite, thats more like separate fields for each category. In theory you'd end up with something like this;

table "categories"
item_no
cat_no / cat_desc

Where "cat_no / cat_desc" is either the category id or the category description (depends on how ambious you are feeling)

- Tony

shady

4:15 pm on Oct 17, 2002 (gmt 0)

10+ Year Member



I generally have a seperate categories, products and product_to_categories_link table.

Your product would have an ID. The categories would have an ID.

The product_to_categories_link table would contain a product_id and a categories_id.

This way, you can assign a single product to appear in an unlimited number of categories

Birdman

4:30 pm on Oct 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So I can have multiple values in a field?

item_no 12345
cats 1 4 8

or..

item_no 12345
cats 1,4,8

Sorry, I'm very green at this. ukgimp, thanks for the links. I'm off to read them.

sun818

4:37 pm on Oct 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have seen one text field used as the category field. Each character was set for "yes" or "no".

Product 1 with YYNNN would mean it was in category 1 and 2.

Product 2 with NNYYN would mean it is in category 3 and 4.

aspdaddy

4:56 pm on Oct 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



IMO You should use a link group,beacause u have a many to many relation :)

Products (id, name, desc, etc..)
Categories (id, name, etc..)
Product_category(id,product_id, catogory_id)

<added>sorry shady, diddnt read your reply there - that is how it should be done , it will give you less problems in the long run</added>

[edited by: aspdaddy at 5:06 pm (utc) on Oct. 17, 2002]

shady

5:06 pm on Oct 17, 2002 (gmt 0)

10+ Year Member



A quick example:

Product table
--------------
Product_id
Product_name
other product fields

Category Table
--------------
Category_id
Category_name

Products_to_categories Table
----------------------------
products_to_categories_id
products_id
categories_id

Create your categories in the categories table.
Create your products in the products table.
Assign products to categories by creating records in the products_to_categories table.
You can therefore assign a product to multiple categories.

When listing products within a particular category, you will access the data via the products_to_categories table.

Hope this helps!

Birdman

6:55 pm on Oct 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Man, what did I get myself into. I'm a little lost but I think it will click soon. Sorry I'm so slow. I added some products to shady's layout, but I still don't get what I'm supposed to enter in the last table. Maybe someone could finish this example and I'll get it.

Product table
--------------
Product_id---------#2684------------#6562------
Product_name---fuzzybluewidget---fuzzyredwidget

Category Table
--------------
Category_id----------1------------------2------
Category_name--fuzzywidgets--------redwidgets--

Prod_to_cat Table
----------------------------
prod_to_cat_id-------1------------------2------
products_id
categories_id

shady

7:09 pm on Oct 17, 2002 (gmt 0)

10+ Year Member



Imagine the products and categories as a totally seperate entity. You are then linking the products to the categories afterwards. In the example, I am demonstrating the functionality of assigning a product to multiple categories.
I would advise you to get a book on database design (in the nicest possible way!)

Product table
--------------
Product_id---------#2684------------#6562-----------#9000
Product_name---fuzzybluewidget---fuzzyredwidget--fuzzyplainwidget

Category Table
--------------
Category_id----------1------------------2-------------3------
Category_name--widgets--------coloredwidgets--plainwidgets----------

Prod_to_cat Table
----------------------------
You now have a widgets category (1) in which all the widgets should appear.
prod_to_cat_id-------1----------2-----------3-------
products_id ---------2684------6562------9000-------
categories_id -------1----------1----------1--------

Also, make the colored widgets appear in the coloredwidgets category,and the plain in the plaincategory
prod_to_cat_id-------4----------5-----------6-------
products_id ---------2684------6562------9000-------
categories_id -------2----------2----------3--------

Dreamquick

7:16 pm on Oct 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Product table
id~~~~description
1~~~~~"fuzzy blue widget"
2~~~~~"fuzzy red widget"
3~~~~~"shiny blue flange"
4~~~~~"shiny red flange"

Category Table
id~~~~description
1~~~~~fuzzy products
2~~~~~shiny products
3~~~~~red products
4~~~~~blue products
5~~~~~widgets
6~~~~~flanges

Prod_to_cat Table
prodid~~catid
1~~~~~~~1
1~~~~~~~4
1~~~~~~~5
2~~~~~~~1
2~~~~~~~3
2~~~~~~~5
3~~~~~~~2
3~~~~~~~4
3~~~~~~~6
4~~~~~~~2
4~~~~~~~3
4~~~~~~~6

So if i wanted to find all the shiny products I'd simply do something like;

SELECT *
FROM Products
WHERE ID IN(
~~SELECT DISTINCT ProdID
~~FROM Prod_to_cat
~~WHERE CatID = 2
)

Which should bring back product ids #3 & #4. Obviously if you wanted to find products which are both Blue & Furry you could then do;

SELECT *
FROM Products
WHERE ID IN(
~~SELECT DISTINCT ProdID
~~FROM Prod_to_cat
~~WHERE CatID = 1
) AND ID IN(
~~SELECT DISTINCT ProdID
~~FROM Prod_to_cat
~~WHERE CatID = 4
)

Which would bring back product id #1 - the only furry and blue product you have, hope this explains it all - sorry for not using your example but it was a little to limited to explain using just red & blue widgets.

- Tony

[edited by: Dreamquick at 7:22 pm (utc) on Oct. 17, 2002]

shady

7:21 pm on Oct 17, 2002 (gmt 0)

10+ Year Member



Good example, Dreamquick! This is turning into an SQL forum

Birdman

7:26 pm on Oct 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you very much! Now I can see how it works. Sorry about being off topic for this forum.

Birdman

4:59 pm on Oct 19, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm back :o

Just wondering if you guys could help me one more time. I followed the above format, but I'm getting an error when I try to veiw the results. This is what I have:

Products table
item_no~~~~description
~~~1~~~~~"fuzzy blue widget"
~~~2~~~~~"fuzzy red widget"
~~~3~~~~~"shiny blue flange"
~~~4~~~~~"shiny red flange"

Categories Table
categories_id~~~~cat_name
~~~~~~~1~~~~~~~fuzzy products
~~~~~~~2~~~~~~~shiny products
~~~~~~~3~~~~~~~red products
~~~~~~~4~~~~~~~blue products
~~~~~~~5~~~~~~~widgets
~~~~~~~6~~~~~~~flanges

Prod_cat Table
item_no~~catid
~~~1~~~~~~~1
~~~1~~~~~~~4
~~~1~~~~~~~5
~~~2~~~~~~~1
~~~2~~~~~~~3
~~~2~~~~~~~5
~~~3~~~~~~~2
~~~3~~~~~~~4
~~~3~~~~~~~6
~~~4~~~~~~~2
~~~4~~~~~~~3
~~~4~~~~~~~6

$query = "SELECT *
FROM Products
WHERE ID IN(
~~SELECT DISTINCT item_no
~~FROM Prod_cat
~~WHERE Cat_id = 1
)";

Appreciate the help!

aspdaddy

5:54 pm on Oct 19, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Birdman,
You can avoid a lot frustration writing SQL by being very consistent when naming fields; all the keys are 'tablename_ID' etc. and either use or dont use abbrevaitions - cat or category. It just makes life easier...

Anyhow, looking at your table/fieldnames, this join should work.

SELECT item_no, description
FROM Products INNER JOIN (Categories INNER JOIN Prod_Cat ON Categories.Categories_ID =Prod_Cat.Catid) ON Products.item_no=Prod_Cat.item_no
WHERE Prod_Cat.Catid=1

Dreamquick

6:59 pm on Oct 19, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Birdman,

What was the error?

- Tony

Birdman

7:16 pm on Oct 19, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /mypage.html on line 24

Thanks for the help with this! I'm getting the same error with aspdaddy's approach also.

jatar_k

10:26 pm on Oct 19, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I assume that is an error from php.

you did it like so

$query = mysql_query("one of the above queries");
while ($row = mysql_fetch_array($query)) //this would be line 24 where $query is invalid
{
do stuff;
}

is that where the error comes from?

have you checked whether the query returns anything?