Jump to content

Creating Category & Subcategory


Dr-SeMSeM

Recommended Posts

Hi all ,im a newbie in PHP programming and im working on programming a personal website for watching videos onlinethe aim of the website or the something i want to know how exactly to create it the following :What is the code allow me from admin panel to add ( Category / Subcategory belong to mail category & so on ) For Example :============================Main Category ( Series )Subcategory ( 2009 )sub-sub catgory ( Prison Break ) ============================Series- 2009-- Prison Break---- Season IV .ie .. i can add infinity numbers of sections and select to which pre exist section created and can modify it later please give me the idea how to create it , and thanks for your advance :)

Link to comment
Share on other sites

There's no simple code, you'll have to design a data structure for that. It's not something that somebody's going to give you already done.The way I'd do it is to have a database where each item has a parent item, which can be another row in the database or it can be the root.

Link to comment
Share on other sites

Like Ingolme said..You need a database table to hold the categories that includes a column to hold the parent category. Each category should have an ID, it's best if you use an int autonumber ID, so when you add categories you would either choose an existing category to use as the parent, or use no parent. When you add the category to the database, if there is no parent then just use 0 for the parent ID, or else use the ID of the parent category.When you write the category list out you're going to need a recursive function that writes out all categories for a certain parent, and for each category it writes it needs to call itself to write out all the categories under that one. To start it all off you have it write out all categories with a parent of 0, so it will write the root categories and for each category it will call itself to write out the child categories. That function will look something like this:

function get_categories($parent = 0, $indent_level = 0){  $result = mysql_query('SELECT id, name FROM categories WHERE parent=' . $parent);  $indent = '';  for ($i = 0; $i < $indent_level; $i++)	$indent .= '-';  while ($cat = mysql_fetch_assoc($result))  {	echo $indent . $cat['name'] . '<br>';	get_categories($cat['id'], $indent_level + 1);  }}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...