Below is my table structure:
Menu table
id title position -------------------- 1 Test home 2 Test2 home
Category
cid name parent parent_menu -------------------------------- 1 ABC 0 1 2 DEF 0 2 3 GHI 1 0 4 JKL 2 0
Category Description
id cat_id catdesc slug ------------------------------- 1 1 ABC_DESC abc 2 2 DEF_DESC def 3 3 GHI_DESC ghi 4 4 JKL_DESC jkl
Menu table menu Menu table treats the menu name as a position.Category table category Category table processes the category name and other parameters. (if parent = 0, then this means that this is the main category, etc.)Category Description table handles the description, slug, and other parameters.
Now I want to display the data as shown below
Name Description Edit Delete /*table headings*/ ------------------------------------------------------- Menu Title: (Test) Main Category Name: (ABC) ------------------------------------------------------ GHI GHI_DESC edit_icon delete_icon ______________________________________________________ Menu Title: (Test2) Main Category Name: (DEF) ------------------------------------------------------ JKL JKL_DESC edit_icon delete_icon
I tried using JOINS and manipulating data in PHP, but no luck.
SELECT * FROM `category` t1 LEFT JOIN `category_description` t2 ON t1.cid = t2.cat_id WHERE 1
Then in PHP I tried as below
<?php $i = 1; foreach($subcat as $sub) { ?> <?php if($sub->parent == 0) { ?> <tr><td><?php echo $sub->name ?></td></tr> <?php } ?> <?php if($sub->parent != 0) { ?> <tr><td><?php echo $sub->name ?></td><td><?php echo $sub->catdesc ?></td> <td>Edit</td><td>Delete</td></tr> <?php } ?> <?php } ?>
And above prints the table as below:
Main Category Name: ABC Main Category Name: DEF ------ GHI GHI_DESC JKl JKL_DESC
Please suggest how to print as needed.
source share