好的,所以我很新codeigniter
从我现在所学到的来看,我不知道如何创建一个动态category -> subcategory -> subsubcategory system
。您能给我一些指导吗...一些参考资料,任何可以指导我应该学习什么来实现这一目标的东西?谢谢
我应该得到我的URL
像这样www.site.com/category/subcategory/subsubcategory/etc...
, 你知道我的意思。
我已经为 PyroCMS 中的页面管理器完成了此操作,但这并不是一件容易的事。
每个页面都有自己的 slug 和 Parent_id,然后为了读取正确的页面,它会循环遍历每个页面 slug 并加入子页面。它知道有多少个孩子,因此如果有 5 个孩子,它会选择第 5 个自连接表。
这是代码示例:
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();
}
这有点疯狂,但效果很好。这可能会非常慢,因此 PyroCMS 还维护一个查找表,其中包含要快速匹配的 id 和页面 URI。
您可以在这里看到整个模型:
http://github.com/philsturgeon/pyrocms/blob/master/application/modules/core/pages/models/pages_m.php http://github.com/philsturgeon/pyrocms/blob/master/application/modules/core/pages/models/pages_m.php
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)