category print

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
achintha
Forum Newbie
Posts: 13
Joined: Mon Apr 30, 2007 10:21 am

category print

Post by achintha »

I want to know how do I print a categories from database.I want to know how to do it for unlimited level. like this

Maincat 1
--Sub Cat 1
------Sub Cat 1
--Sub Cat 2
--Sub Cat 3
Maincat 2
Maincat 3
--Sub Cat 1
Maincat 4
Maincat 5
--Sub Cat 1
-----Sub Cat 1
-----Sub Cat 2
--Sub Cat 2
--Sub Cat 3
Maincat 6

I'm using mysql.
please instruct me from that begining.It will help me more. I have no idea aobut this.

Thanks
Samindika
Last edited by achintha on Tue May 26, 2009 6:26 am, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: category print

Post by onion2k »

You must have some ideas surely?
achintha
Forum Newbie
Posts: 13
Joined: Mon Apr 30, 2007 10:21 am

Re: category print

Post by achintha »

When I google I found the following script. It works.but it showing a error. i have included the code & error which I get

Code:

Code: Select all

<?php
 
// Recursive function to generate a parent/child tree
// Without the need for a Root parent
// Written by: Brian Parnes
// 13 March 2006
 
$connect = mysql_connect('localhost','admin','123');
mysql_select_db('test1');
$nav_query = mysql_query("SELECT * FROM `category` ORDER BY `category_id`");
$tree = "";                 // Clear the directory tree
$depth = 1;                 // Child level depth.
$top_level_on = 1;          // What top-level category are we on?
$exclude = array();         // Define the exclusion array
array_push($exclude, 0);    // Put a starting value in it
 
while ( $nav_row = mysql_fetch_array($nav_query) )
{
    $goOn = 1;          // Resets variable to allow us to continue building out the tree.
    for($x = 0; $x < count($exclude); $x++ )        // Check to see if the new item has been used
    {
        if ( $exclude[$x] == $nav_row['category_id'] )
        {
            $goOn = 0;
            break;              // Stop looking b/c we already found that it's in the exclusion list and we can't continue to process this node
        }
    }
    if ( $goOn == 1 )
    {
        $tree .= $nav_row['title'] . "<br>";                // Process the main tree node
        array_push($exclude, $nav_row['category_id']);      // Add to the exclusion list
        if ( $nav_row['category_id'] < 6 )
        { $top_level_on = $nav_row['category_id']; }
 
        $tree .= build_child($nav_row['category_id']);      // Start the recursive function of building the child tree
    }
}
 
function build_child($oldID)            // Recursive function to get all of the children...unlimited depth
{
    global $exclude, $depth;            // Refer to the global array defined at the top of this script
    $child_query = mysql_query("SELECT * FROM `category` WHERE parent_id=" . $oldID);
    while ( $child = mysql_fetch_array($child_query) )
    {
        if ( $child['category_id'] != $child['parent_id'] )
        {
            for ( $c=0;$c<$depth;$c++ )         // Indent over so that there is distinction between levels
            { $tempTree .= "&nbsp;&nbsp;&nbsp;"; }
            $tempTree .= "- " . $child['title'] . "<br>";
            $depth++;       // Incriment depth b/c we're building this child's child tree  (complicated yet???)
            $tempTree .= build_child($child['category_id']);        // Add to the temporary local tree
            $depth--;       // Decrement depth b/c we're done building the child's child tree.
            array_push($exclude, $child['category_id']);            // Add the item to the exclusion list
        }
    }
 
    return $tempTree;       // Return the entire child tree
}
 
echo $tree;
 
?>
 
 
Result with error

Code: Select all

 
Notice: Undefined variable: tempTree in E:\wamp\www\database\ind2ex.php on line 48
 
Notice: Undefined variable: tempTree in E:\wamp\www\database\ind2ex.php on line 48
 
Notice: Undefined variable: tempTree in E:\wamp\www\database\ind2ex.php on line 57
 
Notice: Undefined variable: tempTree in E:\wamp\www\database\ind2ex.php on line 57
 
Notice: Undefined variable: tempTree in E:\wamp\www\database\ind2ex.php on line 57
 
Notice: Undefined variable: tempTree in E:\wamp\www\database\ind2ex.php on line 48
 
Notice: Undefined variable: tempTree in E:\wamp\www\database\ind2ex.php on line 48
 
Notice: Undefined variable: tempTree in E:\wamp\www\database\ind2ex.php on line 57
 
Notice: Undefined variable: tempTree in E:\wamp\www\database\ind2ex.php on line 57
 
Notice: Undefined variable: tempTree in E:\wamp\www\database\ind2ex.php on line 57
ELECTRONICS
   - TELEVISIONS
      - TUBE
      - LCD
      - PLASMA
   - PORTABLE ELECTRONICS
      - MP3 PLAYERS
         - FLASH
      - CD PLAYERS
      - 2 WAY RADIOS
 
Database

Code: Select all

CREATE TABLE IF NOT EXISTS `category` (
  `category_id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `parent_id` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`category_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
 
--
-- Dumping data for table `category`
--
 
INSERT INTO `category` (`category_id`, `title`, `parent_id`) VALUES
(1, 'ELECTRONICS', '1'),
(2, 'TELEVISIONS', '1'),
(3, 'TUBE', '2'),
(4, 'LCD', '2'),
(5, 'PLASMA', '2'),
(6, 'PORTABLE ELECTRONICS', '1'),
(7, 'MP3 PLAYERS', '6'),
(8, 'FLASH', '7'),
(9, 'CD PLAYERS', '6'),
(10, '2 WAY RADIOS', '6');
 
please help me to fine what is the error
Last edited by Benjamin on Tue May 26, 2009 11:25 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: category print

Post by onion2k »

Those aren't errors, they're notices. The script is working fine. It's just telling you the tempTree variable isn't defined before it's used. To make the notice go away set the variable to something.
Post Reply