Jump to content

MySQL/PHP Help [ordering distinctly]


marcus

Recommended Posts

Here's my code:

 

echo "<table border=0 cellspacing=3 cellpadding=3 width=400>\n";
echo "<tr><td colspan=2 align=center class=bar><font class=barfont>OMG CHART</font></td></tr>\n";

$sql = "SELECT DISTINCT `uid` FROM `trophies` LIMIT 10";
$res = mysql_query($sql) or die(mysql_error());


while($row = mysql_fetch_assoc($res)){
$sql2 = "SELECT COUNT(DISTINCT `id`) AS `counted` FROM `trophies` WHERE `uid`=$row[uid]";
$res2 = mysql_query($sql2) or die(mysql_error());
$row1 = mysql_fetch_assoc($res2);
$num = mysql_num_rows($res2);
$row2 = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `uid`=$row[uid]"));
echo "<tr><td align=center>$row2[username]</td><td align=center>".mysql_result($res2,counted)."</td></tr>\n";
}
echo "</table>\n";

 

What I'm trying to do is order the "$sql2" query by how many trophies they have. I can't seem to do that, I've tried; ORDER BY COUNT(DISTINCT `id`) [that didn't work]

 

Without ordering I get something like:

 

angela 3

kim 3

Sparts 4

egle 3

blue_flame_2k6 3

hxcChaos 5

jason 3

sadie_bledsoe 3

Chad 3

marcus 5

 

All randomly placed, I want it to order ascending. How can I do this?

Link to comment
Share on other sites

Sure thing.

 

  `id` int(11) NOT NULL auto_increment,
  `uid` int(11) NOT NULL default '0',
  `place` int(11) NOT NULL default '0',
  `score` int(255) NOT NULL default '0',
  `game` varchar(255) NOT NULL default '',
  `src` varchar(255) NOT NULL default '',
  `text` text NOT NULL,
  PRIMARY KEY  (`id`)

Link to comment
Share on other sites

SELECT distinct(nick), count(distinct(nick))FROM `PHPAUCTION_users` group by nick order by nick

 

that another sample that i query from my db maybe thats better that doing those repeated query use that as prototype

Link to comment
Share on other sites

That's not going to help me... the only reason I call the user query is to get the username.

 

 

Now from that how do you store the total number of trophies? I'm presuming that uid is the user's ID number?

 

Each trophy is stored INDIVIDUALLY. Each row is unique, so if I have 5 trophies, I would have 5 rows in the database.

Link to comment
Share on other sites

I'm a little confused here as you've got your `id` field in the `trophies` table set to auto_increment so as you enter a trophy the number will increment by one.

 

Yet with your query to sort the trophy scores you seem to be going by that same field.

 

Shouldn't there be a `score` field or something instead?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.