should i loop twice or once???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

should i loop twice or once???

Post by Cruzado_Mainfrm »

ok i have this simple question, and i don't wanna do bechnmarking because it's obvious what's slower, but i wanna know which one is better, for high traffic i might say:

1.for earch letter of the alphabet(A-Z), query the database to return artist names

or

2.query all the artists and sort them in category trees using array functions, and if that's the best way, how? :D

example:
A
-Abba Teens

B
-Britney Spears

C
-Christina Aguilera
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

If you don't need or want to paginate, I'd go for one query.

Write a little fn which checks the first letter, writes a new subhead if it's changed, and does nothing if not. Stick that in the while loop where you run through the result rows.

Depending on your page layout and templating preferences, output buffering might be useful to capture the list as a single block of html (which you can then stick in a template somewhere).
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post by evilMind »

SELECT * FROM `artists_table` ORDER BY `artist_first_name`,`artist_last_name`;
Is that what you were asking?
Post Reply