HOME | DD

CodeIsArt β€” group-by.php:2 by

Published: 2008-02-22 12:37:02 +0000 UTC; Views: 1556; Favourites: 3; Downloads: 65
Redirect to original
Description Often when we are outputting data from our databases, we have the issue of trying to output the HTML in the cleanest method possible. One such nasty mess is often the group-by scenario, whereby one field only changes after so many rows, and not with every single row (like price for example).

An example of this would be a shop where you are outputting different categories of items, and in your HTML you wish to put a title separating each category.

The problem with doing this elegantly, is that in a normal loop you need to know when the group has changed, ahead of actually dealing with the data in the current iteration of the loop. (because if the group changes, the data in your hands must wait until after you've outputted a title)

And then you have the case where the user visits a page where there happens to be only one category, so that your nice code to check for the change of category in the loop has no effect, so you have to repeat the same code again at the end of your loop to catch this condition. (And lord forbid you'd have to use a function to achieve this!)

---

I looked at this problem and simply didn't feel happy repeating a piece of code just to handle this look-ahead condition.

Presented here is a solution that cleanly divides rows into their categories, all without having to do anything after the loop, and without failing on single-category lists.

Enjoi

---

update!
I reduced the code a great deal and done away with the buffer. Now everything echo's in natural order, which is brilliant!
Related content
Comments: 8

JohnJensen [2008-12-03 15:12:14 +0000 UTC]

What program do you use?

πŸ‘: 0 ⏩: 1

CodeIsArt In reply to JohnJensen [2009-02-02 18:13:02 +0000 UTC]

TextMate. macromates.com

πŸ‘: 0 ⏩: 1

JohnJensen In reply to CodeIsArt [2009-02-02 18:20:45 +0000 UTC]

Oh thanks. :)

πŸ‘: 0 ⏩: 0

photofroggy [2008-03-02 00:04:20 +0000 UTC]

Nice work

I really need to learn the MySQL functions in PHP...

πŸ‘: 0 ⏩: 1

CodeIsArt In reply to photofroggy [2008-03-02 06:57:14 +0000 UTC]

Thanks! I also have a piece of code, a database class that's very tight and quite artistic, but I'm weighing up whether it's really too much like sample code, than it is art. Contact me, and I might have something useful to share.

πŸ‘: 0 ⏩: 1

photofroggy In reply to CodeIsArt [2008-03-02 10:30:33 +0000 UTC]

Sweet I reckon really clever stuff could be classed as artistic, so post it somewhere and see what people think! You could always have a group of people giving their opinions on this kind of stuff in the chatroom or something.

Also; thanks for the offer but I prefer learning from my own experiences

πŸ‘: 0 ⏩: 1

CodeIsArt In reply to photofroggy [2008-03-02 11:45:59 +0000 UTC]

"You could always have a group of people giving their opinions on this kind of stuff in the chatroom or something."
#codeisart :B

Experience is seeing how one person does something, and knowing you can do better.

πŸ‘: 0 ⏩: 1

photofroggy In reply to CodeIsArt [2008-03-02 12:53:13 +0000 UTC]

ah, touchΓ© :B

πŸ‘: 0 ⏩: 0