且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

codeigniter category - >子类别>子类别系统

更新时间:2023-11-29 23:29:40

我已经完成了这对于PyroCMS中的页面管理器,但这不是一件容易的任务。



每个页面都有自己的slug和parent_id,然后读取正确的页面,页面子弹和加入孩子。它知道有多少个孩子,所以如果有5个孩子,它选择第5个自联接表。



这里是一个代码示例:

  public function get_by_path($ segments = array())
{
//如果URI作为字符串传递,爆炸创建一个段数组
if(is_string($ segments))
{
$ segments = explode('/',$ segments);
}

//计算出多少段
$ total_segments = count($ segments);

//这是目标别名(树中的最后一页)
$ target_alias ='p'。$ total_segments;

//开始查询,从目标别名中选择(*)从页面
$ this-> db-> select($ target_alias。'。*');
$ it-> db-> from('pages p1');

//循环thorugh each slug
$ level = 1;
foreach($ segments as $ segment)
{
// Current是当前页面,child是下一个要加入的页面。
$ current_alias ='p'。$ level;
$ child_alias ='p'。($ level - 1);

//我们不想再次加入第一页
if($ level!= 1)
{
$ this-> db-> join ('pages'。$ current_alias,$ current_alias。'。parent_id ='。$ child_alias。'。id');
}

//添加slug到where子句以保持我们在正确的树
$ this-> db->其中($ current_alias。'.slug' $ segment);

//增量
++ $ level;
}

//只能是一个结果
$ this-> db-> limit(1);

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

这有点坚果,但它工作完美。这可能真的很慢,所以PyroCMS还维护一个查找表,其id和页面URI匹配很快。



你可以在这里看到整个模型: p>

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


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.

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