I need help. I want to show div element in my Codeigniter PHP page from 3 table relations:
my Table is= Category =>category_id, category_name Sub_Category => sub_category_id,category_id, sub_category_name Item => item_id, sub_category_id,item_name
model =
public function item_data() { $query = $this->db->query('SELECT A.item_name, B.sub_category_name, C.category_name FROM item A sub_kategori A LEFT JOIN sub_category B ON B.sub_category_id = A.sub_category_id LEFT JOIN category C ON C.category_id = B.category_id '); return $query->result(); }
Controller :
function item_view() { //-------- $item_data = $this->model_app->data_ticket(); $data['item_data'] = $item_data; $this->load->view('template', $data); }
View :
<?php $no = 0; foreach($item_data as $row) : $no++;?> <div class="card"> <div class="card-header"> <h3 class=""><?php echo $row->sub_category_name;?></h3> <h5 class=""><?php echo $row->category_name;?></h5> </div> <div class="card-footer"> <ul> //... How to show/Looping item_name by sub_category_id ...? <li class="nav-item"> <a href="#" class="nav-link"> <?php echo $row->item_name;?> </a> </li> </ul> </div> </div> <?php endforeach;?>
Above code show repeated sub_category_name by item_name. output Now=
sub_category1 => category1=>item1 sub_category1 => category1=>item2 sub_category1 => category1=>item3 sub_category2 => category1=>item4 sub_category2 => category1=>item5 sub_category2 => category1=>item6 ...
I want to get achieve data echo =
sub_category1 => category1=>item1,item2,item3 sub_category2 => category1=>item4,item5,item6 .....
Question: How to show/Looping item_name each by sub_category_id …?
Thank’s for help.
Advertisement
Answer
i assume the result of your query would be like:
Iphone Xr 64 GB|Iphone|Apple
Iphone XS 128 GB|Iphone|Apple
Airpod 2| Headphone|Apple
Airpod Pro| Headphone|Apple
Macbook air 2020 128Gb| laptop|Apple
So, if you want display group by sub category, you should restructure your data in model or controller, and don’t do it in the view, because nest php code and html code is very difficult to debug and reusable. to the structure like this:
[Note]
: because the output of your query is stdclass, so let convert it to array, before using my code:
$arr = json_decode(json_encode($yourObject), TRUE);
['Iphone'=> [ 'category'=>'Apple', 'items'=> ['Iphone Xr 64 GB','Iphone XS 128 GB'] ] ]
i have some code for you to group list by sub_category
$arr = [ ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone XR'], ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone XS'], ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone X'], ['category'=>'Apple','sub_category'=>'HeadPhone','item'=>'AirPod 2'], ['category'=>'Apple','sub_category'=>'Laptop','item'=>'Macbook air'], ['category'=>'Samsung','sub_category'=>'MobilePhone','item'=>'Galaxy S10'], ]; $newArr = []; foreach ($arr as $key => $item) { $newArr[$item['sub_category']][$key] = $item; }
the result will be:
$newArr = [ 'Iphone'=>[ ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone XR'], ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone XS'], ['category'=>'Apple','sub_category'=>'Iphone','item'=>'Iphone X'], ], ... ];
the view should be:
<?php foreach($new_arr as $key => $value)?> <div class="card"> <div class="card-header"> <h3 class=""><?php echo $key;?></h3> <h5 class=""><?php echo $value[0]['category'];?></h5> </div> <div class="card-footer"> <ul> <?php foreach($value as $item) ?> <li class="nav-item"> <a href="#" class="nav-link"> <?php echo $item['item']?> </a> </li> <?php endforeach;?> </ul> </div> </div> <?php endforeach;?>