简体   繁体   中英

How to create sub categories of the sub categories

I am creating a store, with categories. What I want to achieve:

Main category
   sub category
       sub category of the sub category
another main one 

At the moment I have a table structure like so:

CREATE TABLE `categories` (
  `category_id` int(11) NOT NULL,
  `main_category` varchar(50) NOT NULL,
  `sub_category` varchar(50) NOT NULL,
  `category_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `categories`
--

INSERT INTO `categories` (`category_id`, `main_category`, `sub_category`, `category_date`) VALUES
(1, 'Electronics', 'Laptops', '2016-02-21 13:49:14'),
--
-- Indexes for dumped tables
--

I want to create sub categories so the user can click the main category which whill show all sub categories or click the sub category which will show the sub categories of the sub category, or click the sub sub category.

In my product table, I cam using the category id so it can be joined n the category, here's my php:

public static function category_form()
{
    echo '<select name="categories" class="form-control col-sm-12">';
    echo '<option>'.System::translate('Choose an option').'</option>';

        $i = 0;
        $mainCatName = '';
        foreach(SELF::categories() as $category): 
            if($category->main_category != $mainCatName) {
                $mainCatName = $category->main_category;
                if($i = 0) { 
                    echo '<optgroup label="'.$category->main_category.'">';
                } else {
                    echo '';
                    echo '<optgroup label="'.$category->main_category.'">';
                }
            }

            echo '<option value="'.System::escape($category->sub_category).'">'.System::escape($category->sub_category).'</option>'; 
            $i++;
            endforeach;
    echo '</select>';

}

使用单个等号=====比较不等于= ,它在每次迭代中将$ i值设置为0 ,将其更改为:

if($i == 0) { 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM