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
https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/
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`)

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.

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.