CodeIgniter Forums

Full Version: removing duplicate MYSQL generated array results
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]wizzer[/eluser]
Hello,

Is there a neat way to do this using CI framework?

I have 3 mysql tables.

my SQL results are like this after left joining tables.

section1 - category1 - type1
section1 - category1 - type2
section1 - category2 - type3

so the section is the primary key of table it has a one to many relationship, ie you can have 1 section to many categories and 1 type to many categories.

Im lost as to how to output this so i can have my results like

section1 - category1 - type1
type2(should be underneath type 1 but it wont stay!)

section1 - category2 - type3

I have been using PHP a while but amazingly never come across this, i think its common.

Can i do this in CI some way or will it be some messy function and I have toplay with arrays and checking for duplicates?

THANKS

El Forum

[eluser]jedd[/eluser]
I normally tidy this up as I'm about to display the data - you're usually talking about a fairly modest quantity, anyway, and I think the overhead of 'tidying up' the array in or near the model is a bit of a waste.

El Forum

[eluser]ronnie_nsu[/eluser]
can you not remove the duplicates using the sql's DISTINCT function ??

El Forum

[eluser]nicholas.byfleet[/eluser]
If you're using Active Record:
Code:
$this->db->distinct();

Hope this helps.

El Forum

[eluser]darkhouse[/eluser]
I think jedd is right, tidy up the array. You wouldn't be able to do that with DISTINCT, the data is technically already DISTINCT. You just need to loop through and keep track of previous sections and categories.

However, I don't really see the difference in overhead if you do it in the model or not. It has to be done somewhere, so might as well do it in the model and just send the appropriate data to the controller, then view.

El Forum

[eluser]wizzer[/eluser]
thanks for all the replies.

El Forum

[eluser]jedd[/eluser]
[quote author="darkhouse" date="1252659376"]
However, I don't really see the difference in overhead if you do it in the model or not. It has to be done somewhere, so might as well do it in the model and just send the appropriate data to the controller, then view.
[/quote]

Yeah, it's a tough one .. I mean, you have to wear the cost somewhere, of course. I like the idea of doing it in the model from an aesthetic perspective - it's the 'right place', so to speak.

The reason *I* would probably do it out in the view is so I could more easily re-use the output of the method in my model. That is, if it's got the full (redundant) selection of data on each 'row', it makes it much easier if you want to extract an element while keeping its context - in other words, you don't have to flatten out its hierarchy again, if you see what I mean.

But yeah, definitely six of one stuff and highly dependent on what else is going on in the app.