![](/img/trans.png)
[英]Woocommerce category display: Show subcategories & products excluding subcategory products
[英]Codeigniter 3 display Main Category with subcategory and Products
我想根據類別和子類別顯示產品。 我有兩張表,一張是類別,另一張是產品。
類別表:
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;
}
嘗試這個..
URL >-example[dot]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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.