dtqqq24248 2011-09-21 15:30
浏览 42
已采纳

Category.php - 产品显示在子类别中,但如果没有子类别则不在类别中

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;
}
  • 写回答

2条回答 默认 最新

  • dongshi2588 2011-09-22 11:21
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow