Category.php - 产品显示在子目录中,但如果没有子类别,则不会显示在类别中
下面是我的 category.php,它使用 switch 语句来显示子类别或产品。当我单击子类别时,它会按预期显示产品。但是,如果该类别没有子类别,则根本不会显示任何产品。
在这种情况下,switch 语句需要更改吗?整个 foreach 循环需要位于 if 语句中,还是数据库有问题< /strong> 和 ids...菜单中我所有的猫都有一个parent_id = 0,子猫对应于类别表的id,产品被分配了一个category_id。这是 JOIN 的事情吗?
<?php
foreach ($listing as $key => $list){
echo "<img src='".$list['thumbnail']."' border='0' align='left' />";
echo "<h4>";
switch($level){
case "1":
echo anchor('welcome/cat/'.$list['id'],$list['name']);
break;
case "2":
echo anchor('welcome/product/'.$list['id'],$list['name']);
break;
}
echo "</h4>";
echo "<p>".$list['shortdesc']."</p><br style='clear:both'/>";
}
?>
非常感谢任何帮助。
$level 位于欢迎控制器中。
function cat($id){
$cat = $this->MCats->getCategory($id);
if (!count($cat)){
redirect('welcome/index','refresh');
}
$data['title'] = "Company |" .$cat['name'];
if ($cat['parentid'] < 1){
// show other cats
$data['listing'] = $this->MCats->getSubCategories($id);
$data['level'] = 1;
}else{
// show products
$data['level'] = 2;
$data['listing'] = $this->MCats->getProductsByCategory($id);
}
$data['category'] = $cat;
$data['main'] = 'category';
$data['navlist'] = $this->MCats->getCategoriesNav();
$this->load->vars($data);
$this->load->view('template');
}
按类别获取产品函数...这将根据产品表中的category_id 选择产品。然而还是没有演出……
function getProductsByCategory($catid){
$data = array();
$this->db->select('id,name,shortdesc,thumbnail');
$this->db->where('category_id',$catid);
$this->db->where('status','active');
$Q = $this->db->get('products');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
Below is my category.php that uses a switch statement to either show subcategories or products. When i click on a subcategory, it displays the products like it's supposed to. However, if the category has NO SUBCATEGORIES, no products will show at all.
Is this a case where the switch statement needs to be altered, the entire foreach loop needs to be in an if statement or is there something wrong with the db and the ids... all of my cats in the menu have a parent_id = 0, subcats correspond to the id for the category table, the products are assigned a category_id. Is this a JOIN thing?
<?php
foreach ($listing as $key => $list){
echo "<img src='".$list['thumbnail']."' border='0' align='left' />";
echo "<h4>";
switch($level){
case "1":
echo anchor('welcome/cat/'.$list['id'],$list['name']);
break;
case "2":
echo anchor('welcome/product/'.$list['id'],$list['name']);
break;
}
echo "</h4>";
echo "<p>".$list['shortdesc']."</p><br style='clear:both'/>";
}
?>
Any help is much appreciated.
the $level is located in the welcome controller.
function cat($id){
$cat = $this->MCats->getCategory($id);
if (!count($cat)){
redirect('welcome/index','refresh');
}
$data['title'] = "Company |" .$cat['name'];
if ($cat['parentid'] < 1){
// show other cats
$data['listing'] = $this->MCats->getSubCategories($id);
$data['level'] = 1;
}else{
// show products
$data['level'] = 2;
$data['listing'] = $this->MCats->getProductsByCategory($id);
}
$data['category'] = $cat;
$data['main'] = 'category';
$data['navlist'] = $this->MCats->getCategoriesNav();
$this->load->vars($data);
$this->load->view('template');
}
get products by category function... This selects the products based on category_id in the products table. still no show though...
function getProductsByCategory($catid){
$data = array();
$this->db->select('id,name,shortdesc,thumbnail');
$this->db->where('category_id',$catid);
$this->db->where('status','active');
$Q = $this->db->get('products');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试执行以下操作。首先检查数据库,请求的类别是否有产品。然后检查它是否有子类别。如果第二个为 false,则选择第一个并显示链接到主类别的产品。
如果主类别没有产品,但它有子类别并且有产品,那么它的工作方式如代码中所述。
如果主类别没有产品,也没有子类别,那么显示它根本没有意义。
Try doing following. First you check in database, does requested category have products. Then check does it have subcategories. If sencond is false, then choose first and display products linked to main category.
If main category has no products, but it does have subcategories and the do have products, then it work as described in your code.
If main category has no products and no subcategories, then there is no point in displaying it at all.
计算模型中返回的子类别数量;如果是0;将
false
返回给控制器。然后进行测试以查看$listing
是数组还是 false,并向用户输出适当的数据或消息。Count the number of sub categotries returned in your model; if it's 0; return
false
to the controller. Then do a test to see if$listing
is an array, or false, and output the appropiate data or message to the user.