我有主要产品类别列出赞
在一个主要产品类别下,也有子产品。
1个蛋糕 (1.1黄油蛋糕)(1.2巧克力蛋糕)
在我看来,我只使用以下代码来回应这些主要产品。
<div class="row">
<?php if(isset($records)) : foreach($records as $row) : ?>
<div class="span2">
<p class="text-center subheader"><?php echo $row->main_products_cat_name; ?>
</p>
<p class="text-center">
<a href="<?php echo base_url()?>product/all_product_data?id=<?php echo $row->main_products_cat_id;?>"><img src="<?php echo base_url(); ?>assests/images/products/main_products/<?php echo $row->main_products_cat_image; ?>" alt="<?php echo $row->main_products_cat_name; ?>" width="200px" height="200px">
</a>
</p>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
有人可以帮助我回应主要产品下的子产品类别。
答案 0 :(得分:2)
如果我认为您的类别表看起来像这样
+-----------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+-------+
| id | tinyint(4) unsigned | NO | PRI | NULL | |
| parent_id | tinyint(4) unsigned | YES | MUL | NULL | |
| name | varchar(255) | YES | | NULL | |
| note | varchar(254) | YES | | NULL | |
+-----------+---------------------+------+-----+---------+-------+
注意你有一个递归关系(parent_id指向id)
现在,让我们用这样的数据填充它
+----+-----------+----------------+------+
| id | parent_id | name | note |
+----+-----------+----------------+------+
| 1 | NULL | bakery | NULL |
| 2 | 1 | Cake | NULL |
| 3 | 1 | Bun | NULL |
| 4 | 1 | Toffee | NULL |
| 5 | 1 | Bread | NULL |
| 6 | 2 | Chocolate cake | NULL |
| 7 | 2 | Butter cake | NULL |
| 8 | 3 | Honey bun | NULL |
| 9 | 5 | Italian bread | NULL |
| 10 | 5 | French bread | NULL |
| 11 | 5 | Bereber bread | NULL |
+----+-----------+----------------+------+
所以现在我们在模型中创建一个获取给定id
的所有子类别的方法型号
<?php
class menu_mdl extends CI_Model
{
public function get_subcategories_id($id)
{
$qry_str = "SELECT
categories.id,
categories.`name` AS name
FROM
categories
WHERE
categories.parent_id = ".$id;
$q = $this->db->query($qry_str);
return $q->result();
}
}
?>
然后我们在控制器menu.php中调用此方法,如此
<强>控制器强>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class menu extends CI_Controller {
public function index()
{
$this->load->model("menu_mdl");
// get all the subcategories of the root category with id = 1
$all_categories_id = $this->menu_mdl->get_subcategories_id(1);
// prepare the menu as an HTML string
$menu = "<ul>";
//loop through level1 then level2 then level3... you can go as many levels as you want!
foreach($all_categories_id as $level1)
{
if($level1->id)
{
$menu .= "<li><a href='#'>".$level1->name."</a></li>";
$sub_level1 = $this->menu_mdl->get_subcategories_id($level1->id);
if ($sub_level1)
{
$menu .= "<ul>";
foreach($sub_level1 as $level2)
{
$menu .= "<li><a href='#'>".$level2->name."</a></li>";
$sub_level2 = $this->menu_mdl->get_subcategories_id($level2->id);
if ($sub_level2)
{
$menu = "<ul>";
foreach($sub_level2 as $level3)
{
$menu .= "<li><a href='#'>".$level3->name."</a></li>";
}
$menu .= "</ul>";
}
}
$menu .= "</ul>";
}
}
}
$menu .= "</ul>";
$data["menu"] = $menu; // <--- this variable has all the menu as HTML string
$this->load->view('menu',$data);
}
}
现在在视图中我们只是通过简单的回声来回显就绪菜单
查看强>
<?php
echo $menu ;
?>
然后输出将如下所示
<ul>
<li><a href='#'>Cake</a></li>
<ul>
<li><a href='#'>Chocolate cake</a></li>
<li><a href='#'>Butter cake</a></li>
</ul>
<li><a href='#'>Bun</a></li>
<ul>
<li><a href='#'>Honey bun</a></li>
</ul>
<li><a href='#'>Toffee</a></li>
<li><a href='#'>Bread</a></li>
<ul>
<li><a href='#'>Italian bread</a></li>
<li><a href='#'>French bread</a></li>
<li><a href='#'>Bereber bread</a></li>
</ul>
</ul>
&#13;
所有这一切
<强>更新强> 你可以使用这个控制器而不是前一个,如果你想(感谢Meh关于递归函数的想法),循环遍历所有子类别,直到最后一个孩子使用函数magix(因为它的魔力:p)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class menu extends CI_Controller {
public function index()
{
$this->load->model("menu_mdl");
// get all the subcategories of the root category with id = 1
$all_categories_id = $this->menu_mdl->get_subcategories_id(1);
// prepare the menu as an HTML string
$menu = "<ul>";
//loop through level1 then level2 then level3... you can go as many levels as you want!
foreach($all_categories_id as $level1)
{
// loop until through subcategories until there is no subcategories
$this->magix($level1,$menu);
}
$menu .= "</ul>";
$data["menu"] = $menu; // <--- this variable has all the menu as HTML string
$this->load->view('menu',$data);
}
private function magix($level_id,&$menu)
{
if($level_id->id)
{
$menu .= "<li><a href='#'>".$level_id->name."</a></li>";
$sub_level = $this->menu_mdl->get_subcategories_id($level_id->id);
if ($sub_level)
{
$menu .= "<ul>";
foreach($sub_level as $level2)
{
$menu .= "<li><a href='#'>".$level2->name."</a></li>";
$sub_level2 = $this->menu_mdl->get_subcategories_id($level2->id);
if ($sub_level2)
{
$this->magix($level_id,$menu);
}
}
$menu .= "</ul>";
}
}
}
}
我希望有帮助
答案 1 :(得分:1)
我分享了自己的get_category()方法。这个想法不是我的。我是从大约一年前不记得的地方得到的。
public static $list;
public static function get_category(){
$map_result = Product::get_category_array();
// it's like select * from categories. with your table structure
foreach($map_result as $row){
$ref = &$refs[$row['cat_id']];
$ref['parent_id'] = $row['parent_id'];
$ref['cat_name'] = $row['cat_name'];
$ref['cat_id'] = $row['cat_id'];
if ($row['parent_id'] == 0){
$list[$row['cat_id']] = &$ref;
}else{
$refs[$row['parent_id']]['children'][$row['cat_id']] = &$ref;
}
}
self::$list = $refs;
function toUL($list){
$class="";
$html = "";
foreach ($list as $value){
if (!empty($value['children'])){
$html .= '<li><a class="dir" href="./index.php?cat='.$value['cat_id'].'">'.htmlspecialchars($value['cat_name'])."</a>";
}else{
$html .= '<li><a href="./index.php?cat='.$value['cat_id'].'">'.htmlspecialchars($value['cat_name'])."</a>";
}
if (!empty($value['children'])){
$html .= "<ul>".toUL($value['children'])."</ul>";
}
$html .= "</li>";
}
return $html;
}
return toUL($list);
}
查看toUL函数以及如何在其中使用它来进行递归列表。