473,383 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

I need a script that displays the tree structure of every category

<?php
/*
I need your help. I'd be very thankfull if write me this script.I
need a script that displays a list of categories and subcategories
like this:

<select name="category">
<option value="1">Main</option>
<option value="2">Main > Computers</option>
<option value="4">Main > Computers > Hardware </option>
<option value="8">Main > Computers > Hardware > PC</option>
<option value="7">Main > Computers > Hardware > Mac</option>
<option value="9">Main > Computers > Hardware > Atari</option>
<option value="11">Main > Computers > Hardware > PC > History of
Pc</option>
<option value="">etc...</option>
</select>
The categories and subcategories details are stored in these two
tables in a MySQL database.
-categories : the categories names and ids.
-cat_relations : the relations between categories.It shows which
subcategory belongs to which category.
The belongings between categories can go very deep and the number of
categories is unlimited. This script will create the two tables and
fill them with sample data. All you need to do is to change the four
variables below. You can send me the script back to this email :
ya******@yahoo.com and thank you in advance.
*/

//Here starts the script. Please change the values of these variables
to fit your settings
$user = "db";
$database = "db";
$server = "localhost" ;
$pwd = "" ;
//Connection to the database that you created
mysql_connect($server,$user,$pwd) ;
mysql_select_db($database);
//Creation of the two tables : categories and cat_relations
$categories = " CREATE TABLE `categories` (`id` INT not null
AUTO_INCREMENT, `name` VARCHAR(100) not null , PRIMARY KEY (`id`),
INDEX (`id`), UNIQUE (`id`)) comment = 'The categories details' ";
mysql_query($categories) ;
$cat_relations = "CREATE TABLE `cat_relations` (`id` INT not null
AUTO_INCREMENT, `daughter_id` INT not null, `mother_id` INT not null ,
PRIMARY KEY (`id`), INDEX (`id`), UNIQUE (`id`)) comment = 'Which
category is the daughter of which category'";
mysql_query($cat_relations) ;

//Filling the two tables with sample data
$cats = array('Main','Computers','Countries','Hardware','S oftware','Programming
languages','Mac','PC','Atari','Winamp','History of the
PC','IBM','Components','High
level','USA','NYC','LA','Manhattan','India','Winzi p');
for ($i=0;$i<count($cats);$i++){
$sql = mysql_query("insert into categories (name)
values('".$cats[$i]."')");
}
mysql_query("insert into cat_relations (daughter_id,mother_id) values
(2,1),(3,1),(4,2),(5,2),(6,2),(7,4),(8,4),(9,4),(1 1,8),(12,8),(13,8),(10,5),(20,5),(14,6),(15,3),(16 ,15),(17,15),(18,16),(19,3)");
//Now you can have a look on them through phpMyAdmin
?>
Jul 17 '05 #1
0 2222

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bola | last post by:
Hi there. U have table structure shown here: category (Int - AutoIncrement) parent_category(Int) Title(string) 1 0 HOME PAGE 2 ...
6
by: Amit Varia | last post by:
So I know you can't outright make a simple tree structure in SQL, but I could sure use some help figuring out how get around it. I wanted to develop a simple picture gallery where you put picture...
13
by: hornedw | last post by:
I have been working on a ecommerce website for myself. What I needed some assistance on was when i was trying to display the categories/subcategories for the different products. I decided to use...
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
5
by: Milan Krejci | last post by:
the thing is that descentant branches i dont want to expand do expand. $id variable contains an array of branches i want the program to go through (alcohol's id -beer id etc) function...
3
by: gmail | last post by:
Hi Friends, Can any body suggest me on this. I am working on .NET 2005 Frame work 2.0 . I am facing problem using the tree stucture. I created the site map.Now i want use the site map in every...
1
by: boarderchic15 | last post by:
Hi- I have the following XML structure: <?xml version="1.0" encoding="UTF-8"?> <content> <type content_type="Listing"/> <faq_listing> <heading>Benefits</heading>...
4
samikhan83
by: samikhan83 | last post by:
hi... i am designing the database for an inventory system while designing it forming tree like structure.... so i am unable to implement tree in database so can anyone help with this.... its like...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.