Codeigniter 3显示带有子类别和产品的主要类别

问题描述 投票:0回答:1

我想根据类别和子类别显示产品。我有两个表,一个是类别,另一个是产品。

类别表:

catid catname   parentid url
 1    LEDS        0      leds
 2    Samsung     1      samsung
 3    LG          1      lg
 4    Cloths      0      cloths
 5    women       4      women
 6    women-ts    5      women-ts

产品:

pid   pname        catid 
  1   samsung 4k    2
  2   lg full hd    3
  3   t-shirt1      6
  4   t-shirt2      6

我想展示这样的产品:

Category page:

If i select LEDS category display all Sub-category Products
  1.Samsung 4k
  2.lg full hd

if i  select Cloths Category display all sub-category products
  1.t-shirt 1
  2.t-shirt 2

我可以获取类别和子类别,但不能获取产品记录。

我在product_model.php中的代码

 public function fetchCategories($parent = 0, $spacing = '', $user_tree_array = '', $catpage = '') {

        if (!is_array($user_tree_array)){
            $user_tree_array = array();
        }
        $this->db->select('*');
        $this->db->from('category');
        if($catpage == ""){
            $this->db->where('parentId', $parent);
        }else {
            $this->db->where('catid', $catpage);
        }

        $this->db->order_by("catId", "ASC");
        $getData = $this->db->get()->result_array();
        //print_r($getData);
        //print_r($getData);
        if(count($getData) > 0) {
            foreach($getData as $value){
                $color = "";
                if($value['parentId'] == 0){
                    $color = "active";
                }
                $user_tree_array[] = array("cId" => $value['catId'], "name" => $spacing . $value['catName'], "color" => $color);
                $user_tree_array = $this->fetchCategories($value['catId'], $spacing . '-->', $user_tree_array);
            }
        }else {
           // $user_tree_array[] = array("cId" => $getData['catId'],"name"=>$getData['catName']);
        }
         return $user_tree_array;
      }
php codeigniter codeigniter-3
1个回答
0
投票

尝试一下..

URL>-例如[dot] com / category /(类别名称)或URLMyController.php

public function category(){
        $getCatName = $this->uri->segment(2);
        $cat = $this->product_model->fetchCategories($parent = 6, $spacing = '', $user_tree_array = '', $catpage = $getCatName);
        $pro = array();
        foreach($cat as $v){
            $data = $this->product_model->getProductByCategory($v['cId']);
            if(!empty($data)){
                $pro[] = $data;
            }
        }
        $this->data['pro'] = $pro;
        (load you view here..)
    }

product_model.php

  public function getProductByCategory($id){
        $this->db->select('*') 
             ->from('products p')
             ->join('category c', 'p.categoryId=c.catId')
             ->where('c.catId', $id);
        return $this->db->get()->result_array();
       }

    public function fetchCategories($parent = 0, $spacing = '', $user_tree_array = '', $catpage = '') {
        if (!is_array($user_tree_array)){
            $user_tree_array = array();
        }
        $this->db->select('*');
        $this->db->from('category');
        if($catpage == ""){
            $this->db->where('parentId', $parent);
        }else {
            $this->db->where('catSlug', $catpage);
        }
        $this->db->order_by("catId", "ASC");
        $getData = $this->db->get()->result_array();
        if(count($getData) > 0) {
            foreach($getData as $value){
                $color = "";
                if($value['parentId'] == 0){
                    $color = "active";
                }
                $user_tree_array[] = array("cId" => $value['catId'], "name" => $spacing . $value['catName'], "color" => $color);
                $user_tree_array = $this->fetchCategories($value['catId'], $spacing . '-->', $user_tree_array);
            }
        }
         return $user_tree_array;
    }
© www.soinside.com 2019 - 2024. All rights reserved.