Skip to content
Advertisement

PHP Looping data from 3 table relation into each div element

I need help. I want to show div element in my Codeigniter PHP page from 3 table relations:

JavaScript

model =

JavaScript

Controller :

JavaScript

View :

JavaScript

Above code show repeated sub_category_name by item_name. output Now=

JavaScript

I want to get achieve data echo =

JavaScript

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

JavaScript

i have some code for you to group list by sub_category

JavaScript

the result will be:

JavaScript

the view should be:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement