Welcome Guest, Not a member yet? Register   Sign In
removing duplicate MYSQL generated array results
#1

[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
#2

[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.
#3

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

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

Hope this helps.
#5

[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.
#6

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

[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.




Theme © iAndrew 2016 - Forum software by © MyBB