Codeigniter 3 отображает главную категорию с подкатегориями и продуктами - PullRequest
0 голосов
/ 16 апреля 2020

Я хочу показать товар по категории и подкатегории. У меня есть две таблицы: одна категория, другая - продукты.

Таблица категорий:

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;
      }

1 Ответ

0 голосов
/ 17 апреля 2020

попробуйте ..

URL> - пример [точка] com / category / (имя категории) или URL MyController. 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;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...