Question

i've N-level of categories in MySql..

id name parent
1  movie  0 
2  bollywood 1 
3  Hollywood 2 
4  Serial    0 
5  Star plus 4 
6  Sony      4 

now i want to show in Movie Table like this

Tv Serial Name         Category

 Mahabharat            StarPlus-> Serial

how to show that ??

Was it helpful?

Solution

I assume the TV Series Mahabharat had the category_id 4.

Once you have that you can iterate through the categories until you find a category with the parent 0.

For example. :

public function getCategories($record)
{
    $category = "";
    $buildCategory = true;
    $id = $record['category_id'];

    while ($buildCategory)
    {
        $category = $this->getCategoryById($id); //Don't use query here, that's not efficient.
        if ($category['parent'] != 0) {
            $category .= $category['name'] . '->';
        } else {
            $category .= $category['name'];
            $buildCategory = false;
        }
    }

    return $category;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top