Jump to content

Categories with Subcategories in table PHP + MYSQL


JacKy_X

Recommended Posts

Hello Freaks;

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

 

Cid = Categories ID

 

Pid = If Category is a Sub-Category , then Pid = that belong which category. ( Also each sub category own Cid.. Main Categories Pid = 0 in database)

 

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

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

And here is my template codes;

<table width="268" border="1">

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

That function and html code it look like this;
 

ylJ2YK9.jpg

 

But I want to make it like this;

 

Ts08ypx.jpg

 

 

 

So how can i match main and sub category eachother in sql query ? 

Edited by JacKy_X
Link to comment
Share on other sites

 

    $query = "SELECT cid, pid, cname, IF(pid=0, cid, pid) as sortID
              FROM categories
              ORDER BY sortID, pid";
    $result = $DB['DB_Database']->query($query);

    while ($row = $DB['DB_Database']->fetch_assoc($result))
    {
        if($row['pid'] == 0)
        {
            //Insert code for displaying parent category
        }
        else
        {
            //Insert code for displaying child cateory
        }
    }
Link to comment
Share on other sites

    $query = "SELECT cid, pid, cname, IF(pid=0, cid, pid) as sortID
              FROM categories
              ORDER BY sortID, pid";
    $result = $DB['DB_Database']->query($query);

    while ($row = $DB['DB_Database']->fetch_assoc($result))
    {
        if($row['pid'] == 0)
        {
            //Insert code for displaying parent category
        }
        else
        {
            //Insert code for displaying child cateory
        }
    }

 

Thank you for answer. But i couldn't make it run.

 

I Forget the mention that i am using also eval for the calling template;

 

Here is my full code

 

function category() {
global $DB;
$category ='';
$categorysql = $DB['DB_Database']->query("SELECT cid, pid, sort, cname FROM categories GROUP BY cid ORDER BY sort ASC");

while ($row = $DB['DB_Database']->fetch_assoc($categorysql))
{
if ($row['pid'] > 0) {

}
else {
$catid = $row['cid'];
$catname = $row['cname'];
eval("\$category_row = \"".$DB['DB_Template']->LoadTemplate('category_row')."\";");
$callrow .= $kategor_row;}
}
if($category_row == '')
{
$category = '';
}
else{
eval("\$category = \"".$DB['DB_Template']->LoadTemplate('category')."\";");
return $category;
}
}

 

I'm putting this codes in a template named as category_row

<table width="268" border="1">

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

And I'm putting this codes in a template named as category

 

<div class="widget">
<h4>Categories</h4>
</div>
$callrow
Edited by JacKy_X
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.