TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-08-2008, 06:42 PM   #1 (permalink)
The Contributor
 
Join Date: May 2008
Posts: 36
Thanks: 5
Kay1021 is on a distinguished road
Help 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
Kay1021 is offline  
Reply With Quote
Old 05-08-2008, 07:36 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-08-2008, 09:03 PM   #3 (permalink)
The Contributor
 
Join Date: May 2008
Posts: 36
Thanks: 5
Kay1021 is on a distinguished road
Default

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
Kay1021 is offline  
Reply With Quote
Old 05-08-2008, 09:17 PM   #4 (permalink)
Jim
The Addict
 
Jim's Avatar
 
Join Date: Nov 2007
Location: the Netherlands
Posts: 281
Thanks: 2
Jim is on a distinguished road
Default

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! :)
__________________
Nunchaku! Who doesn't like martial arts? =)
Send a message via MSN to Jim Send a message via Skype™ to Jim
Jim is offline  
Reply With Quote
Old 05-12-2008, 09:17 PM   #5 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

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.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:24 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design