lukep11a Posted July 19, 2011 Share Posted July 19, 2011 Hi, I am running a fantasy football league and on a users account page I want to display what rank that user currently is overall, does anybody know how I can modify the code below to display the rank of the current user? <?php $query = "SELECT SUM(teams.points) FROM selections, teams WHERE selections.userid = '{$_SESSION['userid']}' AND (selections.groups = teams.groups)"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_assoc($result); ?> <p class="scoring"><?php echo $row['SUM(teams.points)']; ?></p> Quote Link to comment https://forums.phpfreaks.com/topic/242368-display-rank-value/ Share on other sites More sharing options...
AyKay47 Posted July 19, 2011 Share Posted July 19, 2011 without knowing your table setup its hard to say, you could probably grab all the rows and order them by the sum of the points that they have, then print what number row that they are Quote Link to comment https://forums.phpfreaks.com/topic/242368-display-rank-value/#findComment-1244821 Share on other sites More sharing options...
lukep11a Posted July 19, 2011 Author Share Posted July 19, 2011 This is the code that grabs all rows and orders by sum of the points, do you know how I can print what number row the current user is?? <?php $query = "SELECT SUM(teams.points) FROM login, selections, teams WHERE login.userid = selections.userid AND selections.groups = teams.groups GROUP by login.userid ORDER by SUM(teams.points) DESC"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { } ?> Quote Link to comment https://forums.phpfreaks.com/topic/242368-display-rank-value/#findComment-1244858 Share on other sites More sharing options...
Maq Posted July 19, 2011 Share Posted July 19, 2011 Unless you have it in your DB, just use PHP. Have a variable $i before the while and increment it by 1 every loop. Quote Link to comment https://forums.phpfreaks.com/topic/242368-display-rank-value/#findComment-1244862 Share on other sites More sharing options...
lukep11a Posted July 19, 2011 Author Share Posted July 19, 2011 could you show me how to write this please? Quote Link to comment https://forums.phpfreaks.com/topic/242368-display-rank-value/#findComment-1244866 Share on other sites More sharing options...
Maq Posted July 19, 2011 Share Posted July 19, 2011 could you show me how to write this please? Sorry I misunderstood your question. You would have to use MySQL variables like this: http://stackoverflow.com/questions/1416854/how-to-find-out-the-rank-of-a-row-based-on-a-mysql-sum Quote Link to comment https://forums.phpfreaks.com/topic/242368-display-rank-value/#findComment-1244873 Share on other sites More sharing options...
lukep11a Posted July 19, 2011 Author Share Posted July 19, 2011 I have read that and it was helpful, thanks, but it is slightly different to what I am trying to do. I have now included a variable in my code to count the rows, but do you know how I would display the number of the row for the current user (i.e where the session userid = login.userid)? It is currently displaying the number '1' <?php $query = "SELECT SUM(teams.points) FROM login, selections, teams WHERE login.userid = selections.userid AND selections.groups = teams.groups GROUP by login.userid ORDER by SUM(teams.points) DESC"; $result = mysql_query($query) or die(mysql_error()); $counter = 1; // set a counter value $row = mysql_fetch_assoc($result); ?> <p class="scoring"><?php echo $counter; //output the counter ?></p> Quote Link to comment https://forums.phpfreaks.com/topic/242368-display-rank-value/#findComment-1244888 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.