Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7834947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:24:41+00:00 2026-06-02T13:24:41+00:00

I have some troubles with subcategories in my code. My expectation: Bakery Canned food

  • 0

I have some troubles with subcategories in my code.

My expectation:

  • Bakery
    • Canned food
    • Dairy Products
    • Meat
      • subcategory
      • subcategory
      • …
      • subcategory
    • Sweets and Snacks
      • subcategory
      • subcategory
      • …
      • subcategory

MYSQL Table schema:

Categories:

id
category
name
url
type

$res = mysql_query("SELECT `id`, `name`, `url` FROM `categories` WHERE `type`='category' ORDER BY `name` ASC") or die(mysql_error());
while ($arr = mysql_fetch_array($res))
{
    $faq_categ[$arr['id']]['title'] = $arr['name'];
    $faq_categ[$arr['id']]['url'] = $arr['url'];
}
$res = mysql_query("SELECT `id`, `name`, `category`, `url` FROM `categories` WHERE `type`='subcategory' ORDER BY `name` ASC") or die(mysql_error());
while ($arr = mysql_fetch_array($res)) 
{
    $faq_categ[$arr['category']]['items'][$arr['id']]['name'] = $arr['name'];
    $faq_categ[$arr['category']]['items'][$arr['id']]['url'] = $arr['url'];
}
if (isset($faq_categ)) 
{
    foreach ($faq_categ as $id => $temp) 
    {
        $textbuilder .= '<li><a href="/products/'.$faq_categ[$id]['url'].'/all" title="">'.$faq_categ[$id]['title'].'</a>';
        if (array_key_exists("items", $faq_categ[$id])) 
        {
            foreach ($faq_categ[$id]['items'] as $id2 => $temp)
            {
                $textbuilder .= '<small><a href="/products/'.$faq_categ[$id]['url'].'/'.$faq_categ[$id]['items'][$id2]['url'].'" title="">٠'.$faq_categ[$id]['items'][$id2]['name'].'</a></small>';
            }
        }
        $textbuilder .= '</li>';
    }
}

The result: http://www.picupload.us/images/454result.png

Thanks for your time,
Fox Sank

EDIT:

Here is the table

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(10) NOT NULL auto_increment,
  `category` int(10) default NULL,
  `name` varchar(255) NOT NULL,
  `url` varchar(255) NOT NULL,
  `type` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=17 ;

INSERT INTO `categories` (`id`, `category`, `name`, `url`, `type`) VALUES
(1, 1, 'Bakery', 'brakery', 'category'),
(2, 2, 'Dairy Products', 'dairy-products', 'category'),
(3, 3, 'Sweets and Snacks', 'sweets-and-snacks', 'category'),
(4, 3, 'Corn puffs', 'corn-puffs', 'subcategory'),
(5, 3, 'Biscuits', 'biscuits', 'subcategory'),
(6, 3, 'Cakes', 'cakes', 'subcategory'),
(7, 3, 'Pretzels', 'pretzels', 'subcategory'),
(8, 4, 'Canned food', 'canned-food', 'category'),
(9, 5, 'Meat', 'meat', 'category'),
(10, 5, 'Salami', 'salami', 'subcategory'),
(11, 5, 'Sausages', 'sausages', 'subcategory'),
(12, 5, 'Ham', 'ham', 'subcategory'),
(13, 5, 'Delicatessen', 'delicatessen', 'subcategory'),
(14, 5, 'Frankfurters', 'frankfurters', 'subcategory'),
(15, 5, 'Polony', 'polony', 'subcategory'),
(16, 5, 'Smoked', 'smoked', 'subcategory'),
(17, 5, 'Pate', 'pate', 'subcategory');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:24:43+00:00Added an answer on June 2, 2026 at 1:24 pm

    I try this code, its works..
    you will try this…

    CREATE TABLE IF NOT EXISTS `categorylist` (
     `id` int(5) NOT NULL auto_increment,
    `cname` varchar(25) collate utf8_unicode_ci default NULL,
    `pid` int(5) NOT NULL,
    PRIMARY KEY  (`id`),
    KEY `pid` (`pid`)
    ) ;
    
    
    
    INSERT INTO `categorylist` (`id`, `cname`, `pid`) VALUES
    (1, 'Entertainment', 0),
    (2, 'movies', 1),
    (3, 'actor', 2),
    (4, 'actress', 2),
    (5, 'Drama', 1),
    (7, 'sports', 0),
    (8, 'comedian', 2),
    (9, 'political', 0);
    
    
    
    <?php
    include "header.php";
     include "dbconn.php";
    
     $qry="SELECT * FROM categorylist";
     $result=mysql_query($qry);
    
    
     $arrayCategories = array();
    
     while($row = mysql_fetch_assoc($result)){ 
        $arrayCategories[$row['id']] = array("pid" => $row['pid'], "name" =>  $row['cname']);   
      }
    //createTree($arrayCategories, 0);
    
     function createTree($array, $currentParent, $currLevel = 0, $prevLevel = -1) {
    
    foreach ($array as $categoryId => $category) {
    
        if ($currentParent == $category['pid']) {                       
    
            if ($currLevel > $prevLevel) echo " <ul> "; 
    
            if ($currLevel == $prevLevel) echo " </li> ";
    
            echo '<li id="'.$categoryId.'" onclick=child(this.id);><span>'.$category['name'].'</span>';
    
            if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }
    
            $currLevel++; 
    
            createTree ($array, $categoryId, $currLevel, $prevLevel);
    
            $currLevel--;               
        }   
    
    }
    
    if ($currLevel == $prevLevel) echo " </li>  </ul> ";
    
    }   
    ?>
    <div id="content" class="general-style1">
    <?php
     if(mysql_num_rows($result)!=0)
     {
    ?>
    <ul>
        <li id="0" class="root"><span>Categories</span>
        <?php createTree($arrayCategories, 0); ?>
    </li>
    </ul>
    <?php
    }
    ?>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some troubles with positioning my label/password field. With this code they both
I have some troubles with receiving data on server side. Here goes the code:
I have some troubles with this small JavaScript code: var text=Z Test Yeah !
I have some troubles when using javascript code for Facebook invite friends. Details: User
I have some troubles with saving a image from memorystream. Here is my code:
I have some troubles with my school project. I have a class: #include Group.h
I have some troubles with plotting the figure. I have a file with some
I have some troubles mapping a JSON Array to RestKit. This is what the
I have some troubles with getting the data from the website. The website source
I have some troubles with a method having a typed List parameter, inherited from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.