homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

PHP-MySQL Category Listing

 3:38 pm on Apr 15, 2004 (gmt 0)

Hi :)

I am needing to design a simple category listing. I have a categories table in my DB that has a category_id and a parent_id. The parent_id being what lvl and the category_id being the id number if there was a sub category then the parent_id would equal the category_id.

I have been fooling around in PHP - which I'm still new in - but I can't seem to get anything to work. I have found a few examples online in tutorials and scripts. The only problem with them is that they say this isn't the best way to do this and there is a quicker way, but this way is easier.

Well, I'm looking for the hard way >:). I would need it to be quick, so I was wondering what the best way to do this would be.

Can anyone help :)




 10:04 am on Apr 16, 2004 (gmt 0)

this is the way I use categories & subcategories. I do nto garantee this is the best one, but is very easy to use and maintaince.

1. You need to database tables - at the first one you will store the categories and at the second one you will store the subcategories:

create table test_categories (
category_id int(10) not null primary key auto_increment,
category_name varchar(255) not null);

create table test_subcategories (
subcategory_id int(10) not null primary key auto_increment,
subcategory_name varchar(255) not null,
category_id int(10) not null);

The first one will looks like:

* category_id * category_name *
* 1 * name 1 *
* 2 * name 2 *

and the subcategories table:
* subcategory_id * subcategory_name * category_id *
* 1 * sub name 1 * 1 *
* 2 * sub name 2 * 1 *
* 3 * sub name 3 * 1 *
* 4 * sub name 4 * 2 *

to list the categories, only:
$query = "select * from test_categories";

to list the subcategories, under the category_id 1:

$query = "select * from test_subcategories where category_id = '1' ";

to list all the categories and their subcategories:

$query = "select * from test_categories, test_subcategories where test_categories.category_id = test_subcategories.category_id";

I hope this will helps you.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved