tags:

views:

419

answers:

2

ok, so i'm very new to codeigniter and from what i have learned by now i can't figure out how can i create a dynamic category -> subcategory -> subsubcategory system. Can you give me some guidelines please...some references, anything to guide me on what should i learn to accomplish that? thanks

i should get my URL like this www.site.com/category/subcategory/subsubcategory/etc... , you know what i mean.

+1  A: 

you could:

create controller category, reroute some URIs to it and use it's internal logic to parse it's arguments to pick whatever article client requested:

About URLs: http://codeigniter.com/user_guide/general/urls.html

About URI routing: http://codeigniter.com/user_guide/general/routing.html

Adam Kiss
what do you mean by "controller category" ? can you be more specific please?how can i do that, can you give me an example please? thanks
kmunky
seems like you have no idea what is MVC. I advice you to google MVC and read about, then go to codeigniter and read about it's implementation of MVC.
Adam Kiss
i know what a controller is(and i know how to use one with CI, the basics ), i know the principles of MVC, i've read about URLs and routing but your suggestion still won't ring any bell. Though i'm new to MVC and CI.
kmunky
Adam: This is nothing to do with Routing, this is a question about unlimited nesting of records which is not an easy subject for somebody new to PHP or CI. It's not that easy for people used to them either!Either way, it's not something that is covered in either of the links you posted.
Phil Sturgeon
@Phil : if you reroute everything to controller i.e. `categories` and have `function category ($sub = null, $sub2 = null)` - i think it's pretty doable this way.
Adam Kiss
True, but then you are limited to a set number of levels, which from the way the question was asked seems this is not what he wants.
Phil Sturgeon
No harm done using `function category($uristring)`, `function category()` and `get_func_args` or `function category()` and then uri segments. However, i agree that it might not be the best solution possible. That's why I gave you +1 :)
Adam Kiss
Sure func_get_args() could be used, but then you would need to do something like my model method to get the correct data. :-p Thanks for the +1.
Phil Sturgeon
+2  A: 

I have done this for the page manager in PyroCMS but it is no easy task.

Each page has its own slug and parent_id, then to read the correct page it loops through each of the page slugs and joins the child. It knows how many children there are so if there are 5 children it selects the 5th self-joined table.

Here is an example of the code:

public function get_by_path($segments = array())
{
 // If the URI has been passed as a string, explode to create an array of segments
 if(is_string($segments))
    {
     $segments = explode('/', $segments);
    }

 // Work out how many segments there are
    $total_segments = count($segments);

// Which is the target alias (the final page in the tree)
    $target_alias = 'p'.$total_segments;

    // Start Query, Select (*) from Target Alias, from Pages
    $this->db->select($target_alias.'.*');
    $this->db->from('pages p1');

    // Loop thorugh each Slug
    $level = 1;
    foreach( $segments as $segment )
    {
        // Current is the current page, child is the next page to join on.
        $current_alias = 'p'.$level;
        $child_alias = 'p'.($level - 1);

        // We dont want to join the first page again
        if($level != 1)
        {
            $this->db->join('pages '.$current_alias, $current_alias.'.parent_id = '.$child_alias.'.id');
        }

        // Add slug to where clause to keep us on the right tree
        $this->db->where($current_alias . '.slug', $segment);

        // Increment
        ++$level;
    }

    // Can only be one result
    $this->db->limit(1);

    return $this->db->get()->row();
}

It's a bit nuts but it works perfectly. This can be really slow so PyroCMS also maintains a look-up table which has id and the page URI to match quickly.

You can see the whole model here:

http://github.com/philsturgeon/pyrocms/blob/master/application/modules/core/pages/models/pages_m.php

Phil Sturgeon
thanks @Phil for your piece of code, i'll try to break it. hope you can help me if i get stuck ;) i'm not new to PHP,just CI and MVC...so i hope i'll get it :)
kmunky