Click to See Complete Forum and Search --> : Organizing a loop list by name and not by ID


JoelC
04-22-2006, 09:57 AM
I have this code:

elseif ((!isset($SID) OR $SID == NULL) && $OP == 2) { $query = mysql_query ("SELECT member_id, member_name FROM members"); while($r = mysql_fetch_array($query)) { $id = $r['member_id']; $name = $r['member_name']; echo "<A HREF=\"roster.php?op=2&sid=$id\">$name</A><BR>"; } }

What it does, basically, is show me a list of the members I have. Problem is it does it by they're unique IDs, since that's what I use for the site to know which member it is. What I want the list to do, is show an alphabetical list of members instead. How do I do this?

chazzy
04-22-2006, 10:23 AM
order by member_name will sort it by the name. Add ASC to the end for ascending a-z or DESC for descending z-a

orreip
04-22-2006, 10:24 AM
use this query instead:
SELECT member_id, member_name FROM members
ORDER BY member_name DESC;

JoelC
04-22-2006, 11:09 AM
Thanks, that worked wonderfully. I'm trying to add it to another spot too but it doesn't seem to work. The spot's a little different.

$query = mysql_query("SELECT member_id, member_name, race, ts1, ts2, comment FROM members WHERE class = $counter");

I'm designing a World of Warcraft guild roster which is organized by classes. I've created a loop so that each time around it'll echo another class with all its members into the roster (Classes are sorted by number in the database, druid="1", hunter="2" and so on). When I add "ORDER BY member_name ASC" to the code, though, it gives me some errors on the web page, one for each member, I think, since everyone who was under the Classes on the .php page disappeared. This is the error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/fuzzitco/public_html/rcta/roster.php on line 41

line 41 is:

while($r = mysql_fetch_array($query)){


Any ideas?

chazzy
04-22-2006, 11:42 AM
2 suggestions, and I feel like i've been harping on this for the past 3 days (i would say half the posts that i've made have contained this line:)

check that your query actually worked!


$query = mysql_query("SELECT member_id, member_name, race, ts1, ts2, comment FROM members WHERE class = $counter") or die("Error error danger will robinson".mysql_error());

But I think you want single quotes around $counter so like this:



$query = mysql_query("SELECT member_id, member_name, race, ts1, ts2, comment FROM members WHERE class = '$counter'") or die("Error error danger will robinson".mysql_error());

orreip
04-22-2006, 11:48 AM
i don't understand what you mean. I'm guessing that you query don't run when adding ORDER BY. If that is the case i'm guessing the misplacement of that clause. ORDER BY and GROUP BY always come last.
use SELECT member_id, member_name, race, ts1, ts2, comment FROM members WHERE class = $counter ORDER BY member_name

JoelC
04-22-2006, 10:54 PM
Works yet again, thanks guys. The quotes idea was what worked, and what I tried first, thankfully.