Jump to content

[SOLVED] How to Rank users on my website, depending on the average of 3 MySQL columns?


MasterACE14

Recommended Posts

Evening Everyone,

 

How can I assign users a rank(integer) in the `rank` column in the database, depending on what the average is of 3 columns?

 

what I want is users to be ranked from 1 being the best, and going up, higher being worse.

 

so I'll have users like this for example:

 

Rank - User

1        Bob

2        Tom

3        Roger

4        John

5        Sam

6        Chris

 

etc etc.

 

and there ranks are determined by the average of 3 columns, 'column1' + 'column2' + 'column3' / 3

 

How can I go about this? I want this to be automatic(I will have it run in a hourly cronjob).

 

any help is greatly appreciated.

 

Regards ACE

Link to comment
Share on other sites

try this:

 

<?php

// CONNECT TO YOUR DATABASE!

// You need to change the table name to your table name!

$query = mysql_query("SELECT * FROM users");

while($row = mysql_fetch_array($query)){
// edit the column names below
$add = $row['column1'] + $row['column2'] + $row['column3'];
$result = $add / 3;
$user = $row['user'];
mysql_query("UPDATE users SET average = '$result' WHERE user = '$user'");
}

$x = 0;
$select = mysql_query("SELECT * FROM users ORDER BY average DESC");
while($var = mysql_fetch_array($select)){
$person = $var['user'];
mysql_query("UPDATE users SET rank = '$x' WHERE user = '$person'");
$x++;
}

?>

 

Be sure to edit all the information above to fit your MySQL Database Columns!

Link to comment
Share on other sites

try this:

 

<?php

// CONNECT TO YOUR DATABASE!

// You need to change the table name to your table name!

$query = mysql_query("SELECT * FROM users");

while($row = mysql_fetch_array($query)){
// edit the column names below
$add = $row['column1'] + $row['column2'] + $row['column3'];
$result = $add / 3;
$user = $row['user'];
mysql_query("UPDATE users SET average = '$result' WHERE user = '$user'");
}

$x = 0;
$select = mysql_query("SELECT * FROM users ORDER BY average DESC");
while($var = mysql_fetch_array($select)){
$person = $var['user'];
mysql_query("UPDATE users SET rank = '$x' WHERE user = '$person'");
$x++;
}

?>

 

Be sure to edit all the information above to fit your MySQL Database Columns!

 

Works perfectly!!!

 

Thankyou so much!  ;D

 

Regards ACE

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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