Michdd Posted November 30, 2008 Share Posted November 30, 2008 Selected something from the database ordering by a certain row, how can I get the rank for a certain entry? For example a username, and I'm ordering by Join date, and I want to get the user's Rank of joining. Quote Link to comment https://forums.phpfreaks.com/topic/134886-solved-getting-a-rank/ Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 Query: SELECT * FROM users ORDER BY join_date ASC; Code (sort of): //do query, results in $result $rank = 1; while ($row = mysql_fetch_assoc($result)) { //process stuff echo $rank; $rank++; } Quote Link to comment https://forums.phpfreaks.com/topic/134886-solved-getting-a-rank/#findComment-702366 Share on other sites More sharing options...
Michdd Posted November 30, 2008 Author Share Posted November 30, 2008 Query: SELECT * FROM users ORDER BY join_date ASC; Code (sort of): //do query, results in $result $rank = 1; while ($row = mysql_fetch_assoc($result)) { //process stuff echo $rank; $rank++; } I don't want to generate ranks for all, I know how to do that, I want to get a rank for a certain row. Quote Link to comment https://forums.phpfreaks.com/topic/134886-solved-getting-a-rank/#findComment-702377 Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 Could you just use their user_id then? If it's auto-incrementing, it technically IS their "rank" if you're going by the order in which they joined. Quote Link to comment https://forums.phpfreaks.com/topic/134886-solved-getting-a-rank/#findComment-702385 Share on other sites More sharing options...
Michdd Posted November 30, 2008 Author Share Posted November 30, 2008 Could you just use their user_id then? If it's auto-incrementing, it technically IS their "rank" if you're going by the order in which they joined. That was just an example, in reality I don't want to generate their rank by their ID, but rather by another field. Quote Link to comment https://forums.phpfreaks.com/topic/134886-solved-getting-a-rank/#findComment-702386 Share on other sites More sharing options...
DeanWhitehouse Posted November 30, 2008 Share Posted November 30, 2008 We need more information to help you Quote Link to comment https://forums.phpfreaks.com/topic/134886-solved-getting-a-rank/#findComment-702407 Share on other sites More sharing options...
Michdd Posted November 30, 2008 Author Share Posted November 30, 2008 I think I came up with a solution, but could someone confirm this? $username = $_SESSION['username']; $result = mysql_query("SELECT * FROM users ORDER by rep desc") or die(mysql_error()); $rank = 1; while($row = mysql_fetch_array( $result )) { $check = $row['username']; if($check == $username){ echo $rank; } $rank++; } Quote Link to comment https://forums.phpfreaks.com/topic/134886-solved-getting-a-rank/#findComment-702417 Share on other sites More sharing options...
DSGameMaker Posted November 30, 2008 Share Posted November 30, 2008 Looks okay at a glance, probably want to exit that loop once you found the right row, though Quote Link to comment https://forums.phpfreaks.com/topic/134886-solved-getting-a-rank/#findComment-702429 Share on other sites More sharing options...
DeanWhitehouse Posted November 30, 2008 Share Posted November 30, 2008 You could use WHERE in the sql instead of that if statement Quote Link to comment https://forums.phpfreaks.com/topic/134886-solved-getting-a-rank/#findComment-702430 Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 You could use WHERE in the sql instead of that if statement No, he couldn't. Not for what he's trying to do, at least. Although, I can't help but think that there's a better way to do this. Quote Link to comment https://forums.phpfreaks.com/topic/134886-solved-getting-a-rank/#findComment-702449 Share on other sites More sharing options...
DeanWhitehouse Posted November 30, 2008 Share Posted November 30, 2008 Look at his code $username = $_SESSION['username']; $result = mysql_query("SELECT * FROM users ORDER by rep desc") or die(mysql_error()); $rank = 1; while($row = mysql_fetch_array( $result )) { $check = $row['username']; if($check == $username){ echo $rank; } $rank++; } is the same as doing <?php $username = $_SESSION['username']; $result = mysql_query("SELECT * FROM users WHERE username = '".$username."' ORDER by rep desc") or die(mysql_error()); $rank = 1; while($row = mysql_fetch_array( $result )) { echo $users['id']; } ?> I don't see why you need a separate rank. Quote Link to comment https://forums.phpfreaks.com/topic/134886-solved-getting-a-rank/#findComment-702452 Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 Except for the fact that it's not the same thing, why would you think it was the same thing? EDIT: So why would you post code if you didn't even know what he wanted? =/ Quote Link to comment https://forums.phpfreaks.com/topic/134886-solved-getting-a-rank/#findComment-702454 Share on other sites More sharing options...
DeanWhitehouse Posted November 30, 2008 Share Posted November 30, 2008 Only just read what he wanted, so chill out. Quote Link to comment https://forums.phpfreaks.com/topic/134886-solved-getting-a-rank/#findComment-702458 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.