简体   繁体   中英

Categories with Subcategories in table - PHP + MYSQL

I have Categories and Sub-Categories on my database. Database table contain "cid" , "pid" and "cname" columns.

Cid = Categories ID

Pid = If Category is a Sub-Category , then Pid = that belong which category.

Cname = Category Name

For Example Software is a category and id = 15 and "IOS Software is a sub-category and id= 30 and pid = 15

Now I want to show this Cat. And Sub-Cat in a table. Here is my code:

   <?php
  function categories() {
  $categorysql = $DB['DB_Database']->query("SELECT cid, pid, cname FROM categories GROUP BY cid");
       while ($row = $DB['DB_Database']->fetch_assoc($categorysql))
      {
          if ($row['pid'] > 0 {
                      }
                       else {
                              $catid = $row['cid'];
                              $catname = $row['cname']; }
  }

with this code I can show only Main Categories. Sample Pics here:

在此处输入图片说明

But I want to show like this:

在此处输入图片说明

My Table Code here is like this:

<table width="268" border="1">
  <tr>
    <td width="52" rowspan="2" valign="top">Image</td>
    <td width="200" height="23"><a href="{$catid}">{$catname}<a></td>
  </tr>
  <tr>
    <td height="36" valign="top">This ara for sub cat.</td>
  </tr>
</table> 

So how can I do that, any idea?

For easier implementation your can make two quires

This will give all the categories. ( Let say root category pid will be 0

SELECT name FROM categories where pid = 0

In that loop use 2nd query

 SELECT name FROM categories where pid = $catId 

Here is the php code example to populate the array you can use

function runQuery($sql) { global $DB; $dbResource = $DB['DB_Database']->query($sql); $rows = array(); while ($row = $DB['DB_Database']->fetch_assoc($dbResource)) { $rows[] = $row; } return $rows; } function getSubCat($pid) { subCategorySql = "SELECT name FROM categories where pid = "+ $pid; return runQuery($subCategorySql); } $categorySql = "SELECT name FROM categories where pid = 0" $categories = runQuery(categorySql) $i=0; foreach($categories as $aCat) { $categories[$i++]['sub_cat'] = getSubCat($aCat['id']); }

 function runQuery($sql) {  global $DB; $dbResource = $DB['DB_Database']->query($sql); $rows = array(); while ($row = $DB['DB_Database']->fetch_assoc($dbResource)) { $rows[] = $row; } return $rows; } function getSubCat($pid) { subCategorySql = "SELECT name FROM categories where pid = "+ $pid; return runQuery($subCategorySql); } $categorySql = "SELECT name FROM categories where pid = 0" $categories = runQuery(categorySql) $i=0; foreach($categories as $aCat) { $categories[$i++]['sub_cat'] = getSubCat($aCat['id']); } 

now you can use $categories[index]['name'] for category and $categories[index] ['sub_cat'][sub_cat_index][name] for subcategory name.

This is not a good approach though as it will hit database so many time.

I would suggest changing your php code to work with the following sql that uses an OUTER JOIN :

select c.cname, c2.cname subname
from categories c
  left join categories c2 on c.cid = c2.pid
where c.pid is null

This assumes the parent's pid field is null. Alternatively you could use GROUP_CONCAT to return all your subcategories in a single row instead of multiple rows -- this might be easier for your implementation.

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