Creating Cateories With Unlimited Sub-categories

free web hosting
Open Discussion > CONTRIBUTE > The Internet > Web Design

Creating Cateories With Unlimited Sub-categories

xClawx
This tutorial will show you how to create a script so that you can have multiple categories with unlimited sub-categories in each. They can be as deep as you want. This is very useful for organising data/items. A prime example of this is a link directory which has multiple categories.

QUOTE
Ok first of all I will just briefly explain how this will work - the logic behind it. I will then move on to writing the actual script which will manage the categories and put specific data/information into its category.

The Logic Behind Categories
If youve got a fairly decent amount of experience with the php coding language then you more than likely know that it is a very logical language. Because of this you have to try to understand how a script runs in a logical manner. So here is basically how the categories, sub-categories and data is arranged.

Say for example there is one parent (top/main/root) category which has got a catid of 1. Then there is another category which has got its own catid of 2. The second category however has got an extra value which is a parentid. The value of this is 1. This means that the scond category belongs to whichever category has got a catid of 1.

Thats basically it - It may seem a bit confusing but it should all become a lot clearer as we go through the code itself... So lets begin...
The Database

In this tutorial we will be using a mysql database to store all of the category and item information.

If you havent already got a mysql database available then you will need to create one. If you do not know how either take a look at some of our other tutorials or contact your webhost admin.

Once youve got a database sorted you will need to create the tables. There are two. Create them by simply copying and pasting the following SQL queries into phpMyAdmin

CREATE TABLE `categories` (

`catid` INT( 11 ) NOT NULL AUTO_INCREMENT ,

`parentid` INT( 11 ) NOT NULL ,

`name` VARCHAR( 255 ) NOT NULL ,

`description` TEXT NOT NULL ,

PRIMARY KEY ( `catid` )

);

The above will create the table to hold the categories. It has got the following fields:

catid

parentid

name

description

Now for the item table:

CREATE TABLE `items` (

`itemid` INT( 11 ) NOT NULL AUTO_INCREMENT ,

`itemparentid` INT( 11 ) NOT NULL ,

`firstname` VARCHAR( 255 ) NOT NULL ,

`lastname` VARCHAR( 255 ) NOT NULL ,

PRIMARY KEY ( `itemid` )

);

The above will create a table called items with the following fields:

itemid

itemparentid

firstname

lastname

Ok thats the database sorted. Now onto the code for managing the categories...

Managaing & Organising Categories

The first step we need to do is to connect to the database. This can be done by using the following code:

mysql_connect('localhost','username','password');

mysql_select_db('databasename');

?>

You will need to edit the above code and replace the values with your database host, username, password and database name.

Once done the next step is to show all of the main or root categories. Firstly however we need to insert some categories into our database so that we actually have something to output. Run this SQL query in phpMyAdmin just like before

INSERT INTO `categories` ( `catid` , `parentid` , `name` , `description` )

VALUES (

'', '0', 'Main Category', 'This is the description for the main category'

);

You can also create other root categories. To do this simply use the above query and just change the name and description. For it to be a root category however it must have a parentid of 0.

Now for the script itself...

$query = "SELECT * FROM 'categories' WHERE 'parentid' = 0 ORDER BY 'catid' ASC";

$result = mysql_query($query);

while($cat=mysql_fetch_array($result)){

echo "$cat[catname]";

}

?>

The above will simply output all of the parent categories which will have a parentid of 0. Save the above as main.php

Working With Sub-Categories

The next step now is to be able to display the subcategories of a particular category when clicked. This is done by using the following code:

$query = "SELECT * FROM 'categories' WHERE 'parentid' = $c ORDER BY 'catid' ASC";

$result = mysql_query($query);

while($cat=mysql_fetch_array($result)){

echo "$cat[catname]";

}

?>

Save the above as sub.php

Basically what happens is main.php will show you all of the main categories which are linked to sub.php. They also pass the value of c over from main.php to sub.php - This value is the catid which you want to get the subcategories for. So when a link is clicked it will go to sub.php and will display all of the categories which have got a parentid of whatever the clicked catid was.

Ouputting The Correct Items

Now the whole point of categories is to organise stuff right? Well in this example I have just used peoples names as items which are to be organised into categories. You however can of course use something else.

What the script now needs to do is get the items from the database which belong to the selected category.
Here is the code:

$query = "SELECT * FROM `items` WHERE `itemparentid` = $c";

$result = mysql_query($query);

while($item=mysql_fetch_array($result)){

echo $item[firstname]."
".$item[lastname]."";

}

?>

The above code basically selects all items which belong to the selected category whos value is held within the variable $c. It then outputs these to the browser.

The Final Script

Thats pretty much it. We have covered the basics for creating categories with unlimited sub-categories. You should play around a bit with it and customize it to suit your needs. Here is the entire scripts files as a whole:

Firstly index.php

// Connect to database

mysql_connect('localhost','username','password');

mysql_select_db('databasename');

// Output main categories

$query = "SELECT * FROM 'categories' WHERE 'parentid' = 0 ORDER BY 'catid' ASC";

$result = mysql_query($query);

while($cat=mysql_fetch_array($result)){

echo "$cat[catname]";

}

?>

And now sub.php

// Connect to database

mysql_connect('localhost','username','password');

mysql_select_db('databasename');

// Ouput sub-categories

$query = "SELECT * FROM 'categories' WHERE 'parentid' = $c ORDER BY 'catid' ASC";

$result = mysql_query($query);

while($cat=mysql_fetch_array($result)){

echo "$cat[catname]";

}

// Output Category Items

$query = "SELECT * FROM `items` WHERE `itemparentid` = $c";

$result = mysql_query($query);

while($item=mysql_fetch_array($result)){

echo $item[firstname]."
".$item[lastname]."";

}

?>


And thats it. Thats all you need. Good luck and hope it helped!

Notice from BuffaloHELP:
Wasn't it easy to post from copied source? Plagiarizm is forbidden! Even if you wrote it, because it was posted elsewhere first, you must use QUOTE tags. For all codes (PHP, JAVA,etc) use CODE tags. For long codes use CODEBOX. For html codes use HTML tags. Only and last verbal warning issued.

 

 

 


Reply

sxyloverboy
ok copied this from http://www.devdreams.com/tutorials/t/57 and you didnt even bother putting the code in the code tags? sheesh.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. php unlimited category - 10.42 hr back. (1)
  2. php create subcategories - 14.78 hr back. (1)
  3. sub categories in php - 34.05 hr back. (1)
  4. category,sub category and sub sub category tables in sql 2005 - 43.56 hr back. (1)
  5. unlimited sub-categories class php - 47.05 hr back. (1)
  6. php mysql sub category script - 51.31 hr back. (1)
  7. creating sub category php - 51.94 hr back. (1)
  8. create category php how to - 55.05 hr back. (1)
  9. php unlimited categories - 58.42 hr back. (1)
  10. php unlimit category - 60.36 hr back. (1)
  11. main category and sub menu contents with php and mysql - 74.95 hr back. (1)
  12. creating subcategories html - 70.00 hr back. (2)
  13. menu categories subcategories php dreamweaver - 82.43 hr back. (1)
  14. mysql subcategories - 89.79 hr back. (1)
Similar Topics

Keywords : creating cateories unlimited categories

  1. Help Creating A Profile Website - how do i make a profile website (14)
    Ok this is my idea: I am making a website about anime. Its function is add anime, review anime,
    search anime, anime information. Users can make a profile. People can make a profile which they can
    edit to theire likes with html to give it a fancy look.(this is what my problem is about) What i
    would like to know is how can i accomplish this. I already have the database for the anime titles
    etc ready, the only thing that needs to be done is the profile section. If you have any tutorial,
    tip or guide i would really really appriciate it. Thx in advance. ...
  2. Creating A Floating Javascript Docking Menu - (1)
  3. Creating A Good Website, How?! - (18)
    Creating a good website, How?! I looked at many forums, searching on now to create a good
    website, by good I meant good website interface. For example, Trap17 have this amazing flash header,
    and nice design… I searched and searched, I found that many people started with a photoshop
    picture, then they make it come true by requesting a website coder (A.K.A. Programmer) to code the
    whole website for it. If, I said if, I were good a art, I can design a good nice picture off
    photoshop, and I know how to code, does that means I can make a good website? Please post any ...
  4. Creating A Fully Featured Cms - I am in the process of creating a fully featured CMS and need help (0)
    Hello, my name is Derek and I am in the process of creating a CMS. My CMS will be slightly different
    than most, because I am creating it for business purposes. I own a business, Black Development &
    Produktionz, LLC. and I am creating all of my own services that I will need, such POS (web based to
    be integrated with virtually any OS), a scheduling system (for appointment and so that my client
    will be able to schedule based on open time slots), an emailing system (which I know will require
    more than effort than a normal CMS because it will depend on hosting and so forth), ...
  5. Creating A Site For Web Design/development. - (4)
    Hey I've been thinking about starting my own website for website design/development. I can think
    of many different ways I could do this but I'm looking for the fastest, most simple, and most
    effective way of programing it. So let me explain what I want to do with this site. First off I
    will be selling websites, both custom and template. I need to know the best way for people to pay
    for them. I don't know if I should use pay pal or what. I don't have any expirience in that
    area. I also want to try to buy a server and host websites myself. Now I know how ...
  6. General Categories On Nice Website - Contribute - I'm trying to figure out general categories for personal website (0)
    Hello to all surfers.. Surfing a lot? Tired of messed up links, with strange names and organization.
    You think you could categorize internet in few simple categories made for you by you, which others
    might find attractive?! Well help, and contribute. Maybe if we post how would we like our
    categories to look like going to "BASIS" programmers might follow and we could surf even more
    easier. Post your opinions and ideas. From my side of view, and by the content i look most of the
    time, i would categorize nice site in this categories: QUOTE(My MAIN Categories selectio...
  7. A Quick Guide To Creating A 'blog' Website - I use the term 'blog' in the loosest sense of the word (1)
    Hello all, first post here, nice idea you've got going with the whole credit thing /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> So i thought i'd do my
    bit and write up a quick tutorial on what i've found with my time on the net. Before we start,
    i'd just like to say that when i say 'blog', dont think of a site where all you can do
    is type up your life. No. What I mean is intergrating a program into your site that allows you to
    post news with ease while still still being able to keep the site open to other t...
  8. Give Me Suggestions - creating a site (7)
    i am creating a site which aims at webmasters and internet addicts . i need tips on which service
    should i provide through my site so that they keep coming back. also suggest any good scripts which
    i can use in my site....
  9. Creating Login To Your Web Site - (13)
    I am really interested in creating the login for my web site. I have no experience in data base
    building but if some one has good tutorials about how to create the database and setup everything
    for the login, please post the information in here. Specifically I'm interested in setaild
    description of HOW TO CREATE A DATABSE, HOW TO CONNECTED TO YOUR web hosting, WHICH SPECIFIC
    PROGRAMS you need to make it and other usefull info regarding the setup. Thank you very much...
  10. Tips On Creating Your Own Search - PHP/MySQL Integrated search on your site (1)
    I'm not a professional web developer but I've learned a lot since I used Macromedia
    Dreamweaver. The latest version 8 supports PHP 5. If you select PHP as your working language, you
    can use this software to automatically add dynamic content to your website with MySQL as a
    requisite. If you have a MySQL database containing records like a songs information database, then
    you might probably not be able to add a good search page to your website which will search records
    in the database. I faced the same problem, but now I'm able to create one sophisticated datab...
  11. Free Clipart - A lot of Categories (0)
    Just go here: CODE http://www.freeclipartnow.com/ ...



Looking for creating, cateories, unlimited, categories

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for creating, cateories, unlimited, categories

*MORE FROM TRAP17.COM*
advertisement



Creating Cateories With Unlimited Sub-categories



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE