Jump to content

Recommended Posts

Hey,

 

I need help here, im trying to calculate how well a user is doing against other users by adding multipliers to fields to create a total sum.... then order every user with similiar fashion to see what position the user is against other players.

 

My attempt:

 

<?php
// multiplier values //
$Houses = 2;
$Apartments = 0.5;
$Schools = 3;
// end multipliers //

// work out player position against other users//
$Get = mysql_query("SELECT COUNT( (t2.Houses*$Houses) + (t2.Apartments*$Apartments) + (t2.Schools*$Schools) ) + 1 					
                                        AS Position FROM listings t1
				LEFT OUTER JOIN listings t2
				ON t1.Position < t2.Position
				WHERE t1.UserID = '$UserID'")
Or die(mysql_error());

$row = mysql_fetch_assoc($Get); 

// ordinal position //
Echo ordinal ($row['Position']); 
//ordinal function adds a string to the var so 1 becomes 1st - 2 becomes 2nd etc.
?>

 

 

Hope you can help here because at the moment the query is not working at all.

Link to comment
https://forums.phpfreaks.com/topic/182988-user-row-position/
Share on other sites

Hi

 

Use this as a starting point. It will give you a listing of all the users order by score

 

SELECT UserID, SUM((Houses*$Houses) + (Apartments*$Apartments) + (Schools*$Schools) ) AS Score
FROM listings 
GROUP BY Score
ORDER BY Score DESC

 

Quick play and I think this would do it (although I have a feeling I am missing something obvious so hopefully someone will see an easier way of doing it).

 

SELECT COUNT(T1.UserId)
FROM (SELECT UserID, SUM((Houses*$Houses) + (Apartments*$Apartments) + (Schools*$Schools) ) AS Score
FROM listings
GROUP BY Score) T1
CROSS JOIN
(SELECT UserID, SUM((Houses*$Houses) + (Apartments*$Apartments) + (Schools*$Schools) ) AS Score
FROM listings
WHERE t1.UserID = '$UserID'
GROUP BY Score) T2
WHERE T1.Score > T2.Score

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/182988-user-row-position/#findComment-966369
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.