codeigniter category -> subcategory -> subsubcateg

2019-03-14 03:28发布

问题:

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:

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



回答2:

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



回答3:

I agree with Phil's idea and I was also envisioning that you can create a separate module (if you use modular extensions for example) to handle the categories in a generic way. Then you can reuse that module in any other projects. Basically the new module may be able to handle categories and sub-categories (the hierarchy).



标签: codeigniter