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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Murphy

    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 :
    yasbergy@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,$p wd) ;
    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($ca tegories) ;
    $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($ca t_relations) ;

    //Filling the two tables with sample data
    $cats = array('Main','C omputers','Coun tries','Hardwar e','Software',' Programming
    languages','Mac ','PC','Atari', 'Winamp','Histo ry of the
    PC','IBM','Comp onents','High
    level','USA','N YC','LA','Manha ttan','India',' Winzip');
    for ($i=0;$i<count( $cats);$i++){
    $sql = mysql_query("in sert into categories (name)
    values('".$cats[$i]."')");
    }
    mysql_query("in sert into cat_relations (daughter_id,mo ther_id) values
    (2,1),(3,1),(4, 2),(5,2),(6,2), (7,4),(8,4),(9, 4),(11,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
    ?>
Working...