CodeIgniter Forums

Full Version: category wise listing in CodeIgniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
I want a category wise listing in CodeIgniter framework. Output will be like below format. It will be totally dynamic. Values (colors/red/blue/green etc) can be added/updated from back-end. Can anybody provide me the relevant codes for this?
<ul id="mylist">
<li>colors
<ul>
<li>red</li>
<li>blue</li>
<li>green</li>
</ul>
</li>
<li>shapes
<ul>
<li>round</li>
<li>suare</li>
<li>circles</li>
</ul>
</li>

El Forum

[eluser]noideawhattotypehere[/eluser]
If you can make an array from it, gonna store it in database i guess?
id: no need to explain
name: whatever you want to output
parentid: ID of your parent

Code:
public function generate_ul_li($array, $html = '', $parent = 0, $level = 0, $firstloop = TRUE) {
        if ($array == NULL) {
            return false;
        }
        $has_children = false;
        foreach ($array as $key => $value) {
            if ($value['parentid'] == $parent) {
                if ($has_children === false) {
                    $has_children = true;
                    if ($firstloop) {
                        $html .= '<ul id="mylist">';
                    } else {
                        $html .= '<ul>';
                    }
                    $level++;
                }
                 $html .= '<li>$value['name'] </a>';
                $this->generate_ul_li($array, $html, $value['id'], $level, FALSE);
                $html .= '</li>';
            }
        }
        if ($has_children === true)
            $html .= '</ul>';
        return $html;
    }

call it generate_ul_li($yourarraywithvalues);