SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Categorising Data Into A MySQL Database Using PHP

    Hi,

    I have a database of products but I am finding it diffult to understand how to categorise each product and build a site for it.

    Can I do something like this:

    $category = (

    $gamesconsoles = ( playstation, xbox, wii )
    $personalhygiene = ( electric toothbrush, electric razor, electric shaver)

    )

    So I have PHP code which identifies a 'Playstation' as a $gamesconsoles. It then enters a 'gamesconsoles' tag into the database in a category field.

    I then I have link on my site which looks something like this on the homepage:

    productrange.php?category="gamesconsoles" class="menulink">Games Consoles</a>

    I then link to a productrange.php page which has in it:

    $category = category;
    if (isset($_GET['category'])) {
    $id = $_GET['category'];
    }

    I then echo all the producys from the database which come under the category of 'gamesconsoles'.

    Or am I completely mad and it should be done differently. If so please advise.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    If you haven't already, you should start by designing your database, the PHP code will come later.
    You say you have a database of products. What tables do you have in your database?

  3. #3
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Many thanks,

    I have description, price, full description, image link, link.

    I can enter a category such as Playstation but I dont know how to put this into a category of 'Games Consoles'.

    All I currently have is a sub-category.

    Im looking to do this on multiple basis. For example sub-category of 'xbox controller' into 'Games Controllers'.

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    I have description, price, full description, image link, link.
    These are not tables, they are columns of one table.

    So you have a products table.

    You'll need a categories table.

    And you'll have to decide:
    1) A product can be part of only one category -> add a categoryid column to the products table
    2) A product can be part of more categories -> you'll need a productcategory table (which contains two columns: productid and categoryid) that will link products and categories together

    I can enter a category such as Playstation but I dont know how to put this into a category of 'Games Consoles'.

    All I currently have is a sub-category.

    Im looking to do this on multiple basis. For example sub-category of 'xbox controller' into 'Games Controllers'.
    Take a look at this explanation written by r937: http://sqllessons.com/categories.html

  5. #5
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for this,

    This complicates things. I thought I could this all within the same table.

    So I would have one column for category and one for subcategory. The column for the category would have an entry based on what was in the subcategory.

    This sounds much easier lol

    So I take it I need to link the two tables together somehow?

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    You've lost me here.

    Please post an example of what you would store in your tables.

  7. #7
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Table:


    Category: Sub-Category: Price: Description:
    Games Console Playstation 3 399.99 Playstation 3 Games Console with 3 Games
    Games Controller XBox Controller 29.99 Wireless Xbox Contoller
    Furniture TV Stand 249.99 Television Stand for 32" Flat Panel TV

    So I would enter the category into a 'Category' column based on what is in the Sub-Category

    For example when the code reads 'TV Stand' it would enter Furniture into the Category column.

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    All in one table? Did you read my post and the article I linked to?

  9. #9
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I was just trying to explaing how I thought it was done but I must be wrong. Working with two tables sounds complicated.

    One category sounds fine. So that can be done it one table. Im getting a bit confused here.

    Is it not possible to add a category to one table and allocate a category. That so much easier than using two tables.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •