TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Categories/Sub Categories (http://www.talkphp.com/absolute-beginners/2781-categories-sub-categories.html)

Kay1021 05-08-2008 06:42 PM

Categories/Sub Categories
 
I'm fairly new to php. I was wondering if someone might be able to offer some assistance.


-I have 11 categories and 176 sub categories.
-The sub categories may appear in one or more of the main categories.
-In each sub category is one company (information)

I was wondering how i might go about displaying a list of the subcategories when clicking on main category.


Thank you

Wildhoney 05-08-2008 07:36 PM

You would need to pass the category ID around, perhaps in a GET. Such as like: index.php?category_id=6. You would then use that ID to get the sub categories. All the sub categories should have a parent category ID.

sql Code:
SELECT
    name
FROM
    sub_categories
WHERE
    category_id = 6

Kay1021 05-08-2008 09:03 PM

The only problem is I need it all one one page

just you click on the main category and a list of the subcategories will appear.

As well i'm not sure how the database would be set up

right now i have a table called company
with name, address, website, email


I am having so much trouble with this. As well i am understanding how the select statements work...but not how you output the results.

Thank you

Jim 05-08-2008 09:17 PM

I tried google for a second but didn't found a tutorial that fast.

What Wildhoney means with the ID is a sort of linking. A way to link a company to a category by giving the category an ID.

Let's say you have 5 categories in table 'categories':

Title: Internet
ID: 1

Title: Cars
ID: 2

Title: Hardware
ID: 3

Title: Bikes
ID: 4

Title: Software
ID 5

Each category has it's unique ID.

Now lets say we would like to add some companies to table "company".

Title: Adobe
Category-ID: 5

Title: Yamaha
Category-ID: 4

Title: Ducati
Category-ID: 4

Title: Google
Category-ID: 1

Title: Asus
Category-ID: 3

You now linked all companies to a category. So now when you want to list all companies from the category "bikes". You need to query the database for all companies where the Category-ID is 4.

I highly recommend you to search for a good tutorial for this (with more database tips). Because in the end the database layout is one of the most important things of a big and good working system! :)

drewbee 05-12-2008 09:17 PM

I always do it like this:

category_id | category_name | category_description | category_parent | category_path

1 | Programming | Programming Tutorials | 0 | 0
2 | PHP | PHP tutorials | 1 | 0 1
3 | Javascript | Javascript Tutorials | 1 | 0 1
4 | PHP Time | PHP Time Tutorials | 2 | 0 1 2

So lets say I want 'programming' subcategories.
select category_id, category_name, category_path from cateogires WHERE category_parent = '1'

and to do the breadcrumbs, I would split 'category_path' into the following format '0', '1' so then I could do
SELECT category_id, category_name, category_path FROM categories WHERE category_id IN ('0', '1').

You will need to keep an array to make sure the rows returned are forced into the correct order for hte breadcrumbs though.

For this, it takes two queries to get the current category and subcategories.


All times are GMT. The time now is 07:35 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0