marcus Posted July 8, 2007 Share Posted July 8, 2007 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 More sharing options...
teng84 Posted July 9, 2007 Share Posted July 9, 2007 simply add order by from the query where you are viewing those name Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292782 Share on other sites More sharing options...
marcus Posted July 9, 2007 Author Share Posted July 9, 2007 That makes no sense to me [iMO]. Explain further. Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292784 Share on other sites More sharing options...
teng84 Posted July 9, 2007 Share Posted July 9, 2007 sample SELECT * FROM `users` WHERE `uid`=$row[uid] order by name asc or SELECT * FROM `users` WHERE `uid`=$row[uid] order by name desc Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292785 Share on other sites More sharing options...
marcus Posted July 9, 2007 Author Share Posted July 9, 2007 I'm trying to order it by the amount of trophies they have... not by their name. Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292787 Share on other sites More sharing options...
teng84 Posted July 9, 2007 Share Posted July 9, 2007 that was the idea you can apply them on any case SELECT COUNT(DISTINCT `id`) AS `counted` FROM `trophies` WHERE `uid`=$row[uid] oder by the field name you want Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292789 Share on other sites More sharing options...
marcus Posted July 9, 2007 Author Share Posted July 9, 2007 You're getting no where with me, the amount of trophies, I know how to ORDER by a specific field name, but while trying to order from the AMOUNT of something is completely different. Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292791 Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 Whichever field you have as the number of trophies simply order by that. How is this number of trohies stored? Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292794 Share on other sites More sharing options...
teng84 Posted July 9, 2007 Share Posted July 9, 2007 you place those query inside the loop so what else do you expect Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292797 Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 Any chance you can show the structure of the trophies table? Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292799 Share on other sites More sharing options...
marcus Posted July 9, 2007 Author Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292801 Share on other sites More sharing options...
teng84 Posted July 9, 2007 Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292804 Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 Now from that how do you store the total number of trophies? I'm presuming that uid is the user's ID number? Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292805 Share on other sites More sharing options...
marcus Posted July 9, 2007 Author Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292807 Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 SELECT COUNT(DISTINCT `id`) AS `counted` FROM `trophies` WHERE `uid`=$row[uid] ORDER BY `counted` Would that work? Never done a query sorting data like that as I've organised my databases very differently! Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292812 Share on other sites More sharing options...
marcus Posted July 9, 2007 Author Share Posted July 9, 2007 That wouldn't work because that would ORDERING it by that number, not by a field in the database. Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292814 Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 Yeah I can see that now I look at your code a little more - was just about to say it won't work and you beat me to it! Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292816 Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292820 Share on other sites More sharing options...
marcus Posted July 9, 2007 Author Share Posted July 9, 2007 We don't use scores, trophies are given at a certain score, but no score is recorded. I'm counting the unique IDs PER user Link to comment https://forums.phpfreaks.com/topic/58995-mysqlphp-help-ordering-distinctly/#findComment-292825 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.