Categories with Subcategories in table - PHP + MYSQL

佐手、 提交于 2019-12-12 02:31:46

问题


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?


回答1:


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
  • SQL Fiddle Demo

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.




回答2:


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']);
} 

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.



来源:https://stackoverflow.com/questions/17892855/categories-with-subcategories-in-table-php-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!