tapan
tapan

Reputation: 23

Php code to display subcategories under category

I am fetching the category and subcat information from database as follows:

$cols=array("id","name","description","tags","subcat","img");
$result=$conn->get("category",null,$cols);`

Note that result is a multidimentional array like $result[0]['name'] will display category name and $result[0]['subcat'] will display first category and subcategory respectively. Here is the rest of the snippet:

$subcat="Notset";
$cat=array();
$sub=array();
$j=0;
for ($i=0;$i<$conn->count; $i++)
{ 
    if($result[$i]['subcat'] != $subcat)
    {
        $subcat=$result[$i]['subcat'];
        $sub[$j]=$subcat;
        $j++;
    }
$cat[$i]=$result[$i]['name'];
}

But i an getting result as follows: Snapshot of web page

enter image description here

But i want to display sony under desktop category and macbook under laptop category please help.In the above image red button is category green is for subcategory.

Upvotes: 2

Views: 2846

Answers (2)

jetblack
jetblack

Reputation: 590

You can use the following code: (You'll need to set $fetched_categories = $result in your example.)

function get_subcategories($array, $category_name){
    $return = array();

    foreach ($array as $key => $category){
        if ($category['subcat'] == $category_name){
            $category['sub_categories'] = get_subcategories($array, $category['name']);            
            $return[] = $category;
        }
    }

    return $return;
}

function print_categories($array, $level = 0){
    echo ($level > 0 ? '<ol>' : '<ul>');

    foreach ($array as $category){
        echo '<li>' . $category['name'] . '</li>';
        print_categories($category['sub_categories'], ++$level);
    }

    echo ($level > 0 ? '</ol>' : '</ul>');
}

$fetched_categories = array();
$fetched_categories[] = ['name' => 'Camera', 'subcat' => ''];
$fetched_categories[] = ['name' => 'Macbook', 'subcat' => 'Laptops'];
$fetched_categories[] = ['name' => 'Laptops', 'subcat' => ''];
$fetched_categories[] = ['name' => 'Desktops', 'subcat' => ''];
$fetched_categories[] = ['name' => 'Sony', 'subcat' => 'Desktops'];
$fetched_categories[] = ['name' => 'DSLR', 'subcat' => 'Camera'];

$categories = get_subcategories($fetched_categories, '');

print_categories($categories);

Here is the output:

https://eval.in/734955

Upvotes: 0

petebolduc
petebolduc

Reputation: 1263

I accomplish this using the following php code:

# categories with a parent id of 0 are parent categories 
$selectparents = $db->prepare("
SELECT category_id, category_name 
FROM categories 
WHERE  parent_category_id = 0 
ORDER BY category_name
"); 

$selectparents->execute();

while($row = $selectparents->fetch(PDO::FETCH_ASSOC)){
        $p_category_id = $row['category_id'];
        $category_name = $row['category_name'];

        echo '<b>'.$category_name.'</b><br>';

            # get sub categories for this parent
            $selectchilds = $db->prepare("
            SELECT category_id, category_name 
            FROM categories 
            WHERE  parent_category_id = ".$p_category_id." 
            ORDER BY category_name
            ");
            $selectchilds->execute();

            while($row = $selectchilds->fetch(PDO::FETCH_ASSOC)){
            $category_id = $row['category_id'];
            $category_name = $row['category_name'];

            echo '<b>'.$category_name.'</b><br>';

            }   
}  

Basically all parent categories have a parent_id set to 0 which are queried in alphabetical order and then subs for each are queried within while loop for the parents and are rendered in alphabetical order under the parent.

Upvotes: 1

Related Questions