Welcome Guest, Not a member yet? Register   Sign In
Subcategories - a better way than mine
#1

[eluser]iDenta[/eluser]
Hi!

I am working on a small adminarea for one of my projects, there i wan't to show all of my categories, and subcategories. So the function is only used when in the adminarea.

I have hacked together a controller that takes care of that:

Code:
//Get Level 1
$this->db->where("parent_id", 0);
$this->db->order_by("id", "asc");
$query = $this->db->get('categories');
        
if ($query->num_rows() > 0)
{
    foreach ($query->result() as $row):
            
    $data['cat_title'] = $row->title;
    echo $data['cat_title']."<br />";
            
        // Get Level 2
        $this->db->where("parent_id", $row->id);
        $this->db->orderby("id", "asc");
        $query2 = $this->db->get("categories");

            foreach ($query2->result() as $row2):
            $data['subcat_title'] = $row2->title;
            $data['subid'] = $row2->parent_id;
            echo "---".$data['subcat_title']."<br />";
                    
                // Get Level 3
                $this->db->where("parent_id", $row2->id);
                $this->db->orderby("id", "asc");
                $query3 = $this->db->get("categories");

                foreach ($query3->result() as $row3):
                    $data['subsubcat_title'] = $row3->title;
                    echo "------".$data['subsubcat_title']."<br />";
                endforeach;
            endforeach;
        endforeach;
}

And that spits out something like:

Category 1
-- Subcategory
Category 2
-- Subcategory
-- Subcategory
---- Subsubcategory
Category 3
Category 4
-- Subcategory

And so on.

But i feel that this could be made in a much nicer way... Although it does work, it doesn't seem very efficient.

Since i am a complete newbie, this is about the best i could do Smile So any pointers on making this code look better is appreciated.

And ultimately i would like to be able to go deeper than 3 steps, but that isn't my main problem...


Messages In This Thread
Subcategories - a better way than mine - by El Forum - 03-10-2009, 12:30 PM
Subcategories - a better way than mine - by El Forum - 03-10-2009, 12:42 PM
Subcategories - a better way than mine - by El Forum - 03-10-2009, 12:56 PM
Subcategories - a better way than mine - by El Forum - 03-10-2009, 01:02 PM
Subcategories - a better way than mine - by El Forum - 03-10-2009, 02:22 PM
Subcategories - a better way than mine - by El Forum - 03-10-2009, 09:25 PM



Theme © iAndrew 2016 - Forum software by © MyBB