Maverickb7 Posted April 23, 2006 Share Posted April 23, 2006 I have a table full of lots of rows. I want to tell some code to look at one column (which is a number) and then rank it X out of all the rows. So lets say I have 200 rows and I ask it to look at a row that includes the 5th highest number out of all the rows. I want it to output 5. Anyone know how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/8162-solved-ranking/ Share on other sites More sharing options...
Barand Posted April 23, 2006 Share Posted April 23, 2006 ExampleI have a table of scores with column called score. The 5th highest value in that table is 37581.[code]$scoreval = 37581;$sql = mysql_query("SELECT COUNT(*)+1 FROM scores WHERE score > '$scoreval'");echo "$scoreval is ranked ". mysql_result ($sql, 0);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8162-solved-ranking/#findComment-29769 Share on other sites More sharing options...
Maverickb7 Posted April 23, 2006 Author Share Posted April 23, 2006 [!--quoteo(post=367638:date=Apr 23 2006, 04:13 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 23 2006, 04:13 AM) [snapback]367638[/snapback][/div][div class=\'quotemain\'][!--quotec--]ExampleI have a table of scores with column called score. The 5th highest value in that table is 37581.[code]$scoreval = 37581;$sql = mysql_query("SELECT COUNT(*)+1 FROM scores WHERE score > '$scoreval'");echo "$scoreval is ranked ". mysql_result ($sql, 0);[/code][/quote]Alright... that code seems to do what I want but I have a problem. I don't want to manually add the $scoreval as that number is going to increase very often and very fast. I have a row that includes the number of comments a user has posted and when a user is logged in I want it to rank the user x out of all members. I want it to rank the user by the comment count so I do not want to specify the $scoreval as the highest number is going to change constantly. Quote Link to comment https://forums.phpfreaks.com/topic/8162-solved-ranking/#findComment-29827 Share on other sites More sharing options...
Orio Posted April 23, 2006 Share Posted April 23, 2006 This will be your statement:$query=[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]`table`[/color] [color=green]ORDER BY[/color] [color=red]'score'[/color] [!--sql2--][/div][!--sql3--]so- $result=mysql_query($query);And then :[code]<?php$num=$_GET['row_number'];$i=0;$rows=mysql_num_rows($result);while($i<$rows){$id=mysql_result($result,$i,"ID");if($id==$num){$i=$row; //break while loopecho ($i);}else{$i++};}};[/code]Hope it helps,Orio. Quote Link to comment https://forums.phpfreaks.com/topic/8162-solved-ranking/#findComment-29836 Share on other sites More sharing options...
Maverickb7 Posted April 23, 2006 Author Share Posted April 23, 2006 I have no clue what your code is doing... could you explain it a bit? (Sorry.. still learning and want to know what i'm looking at before putting it to use) Quote Link to comment https://forums.phpfreaks.com/topic/8162-solved-ranking/#findComment-29838 Share on other sites More sharing options...
Orio Posted April 23, 2006 Share Posted April 23, 2006 Well first it gets from the table everyone ordered by their score.Then this line:$num=$_GET['row_number'];Is defining $num as the row number you want (I assumed you do it with a form).And then this part:[code]$i=0;$rows=mysql_num_rows($result);while($i<$rows){$id=mysql_result($result,$i,"ID");if($id==$num){$i=$row; //break while loopecho ($i);}else{$i++};}};[/code]Starts a loop- $rows is the number of results you got from the query and i is just 0. It basicly checks every row and row from its ID column value (mysql_result($result,$i,"ID")) and checks if what you asked for is the samw as the ID we got. If it is, it brakes the loop and echos the ID number. Go to www.php.net and search the mysql_result function if you are still not understanding.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/8162-solved-ranking/#findComment-29845 Share on other sites More sharing options...
Maverickb7 Posted April 23, 2006 Author Share Posted April 23, 2006 [!--quoteo(post=367718:date=Apr 23 2006, 12:54 PM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ Apr 23 2006, 12:54 PM) [snapback]367718[/snapback][/div][div class=\'quotemain\'][!--quotec--]Well first it gets from the table everyone ordered by their score.Then this line:$num=$_GET['row_number'];Is defining $num as the row number you want (I assumed you do it with a form).And then this part:[code]$i=0;$rows=mysql_num_rows($result);while($i<$rows){$id=mysql_result($result,$i,"ID");if($id==$num){$i=$row; //break while loopecho ($i);}else{$i++};}};[/code]Starts a loop- $rows is the number of results you got from the query and i is just 0. It basicly checks every row and row from its ID column value (mysql_result($result,$i,"ID")) and checks if what you asked for is the samw as the ID we got. If it is, it brakes the loop and echos the ID number. Go to www.php.net and search the mysql_result function if you are still not understanding.Orio.[/quote]so how does this do what I asked? I want this to check a certain column in my table tow and tell me what rank it is out of all the rows. Let's say I have 8 members with the following details....username | posts------------------------------------user1 | 10user2 | 123user3 | 3423user4 | 4user5 | 583user6 | 69user7 | 94user8 | 383now lets say user8 is logged in... I want it to check the posts column and compare it to all the other users. Then it should output, "You are ranked #6 out of #8". Get what I'm saying? Quote Link to comment https://forums.phpfreaks.com/topic/8162-solved-ranking/#findComment-29849 Share on other sites More sharing options...
Barand Posted April 23, 2006 Share Posted April 23, 2006 Assuming that table is called "post_totals"[code]$res = mysql_query("SELECT COUNT(*) FROM post_totals");$total_users = mysql_result($res, 0);$username = 'user8';$res = mysql_query("SELECT COUNT(*)+1 FROM post_totals WHERE posts > (SELECT posts FROM post_totals WHERE username = '$username') ");$rank = mysql_result($res, 0);echo "You are ranked #$rank out of $total_users";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8162-solved-ranking/#findComment-29868 Share on other sites More sharing options...
Maverickb7 Posted April 23, 2006 Author Share Posted April 23, 2006 Thanks Barand I'll give it a try. Quote Link to comment https://forums.phpfreaks.com/topic/8162-solved-ranking/#findComment-29869 Share on other sites More sharing options...
Maverickb7 Posted April 24, 2006 Author Share Posted April 24, 2006 Works perfect.. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/8162-solved-ranking/#findComment-30179 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.