SunQuest
           PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old July 4th, 2002, 05:17 PM
mr_fitz mr_fitz is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 13 mr_fitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
detecting duplicate values and stalling $rank counter?

Spreadsheets like Excel do a nice job of detecting if there are duplicate values with their rank function - this is what I would like to mimic.

This is my small script that detects the rank position of a variable Hank_Wins in a mysql database:


php Code:
Original - php Code
  1. $sql="SELECT Name, Hank_Wins FROM mydatabase ORDER BY Hank_Wins ASC";
  2. $result=MYSQL_QUERY($sql);
  3. $rank=1;
  4. while($var=MYSQL_FETCH_ARRAY($result)){
  5. $namemember=$var["Name"];
  6. if ($namemember==Robert)
  7. $hankrankRobert=$rank;
  8. $rank++;
  9. }
  10. //just for fun an echo for Robert's data
  11. echo $hankrankRobert;


which, if you spit out the whole array, for Name Hank_Wins and $rank not just Robert's rank on Hank_Wins you would get something like (formatted better):

+-------------+-------+------+
| Name | Wins| rank |
+-------------+-------+------+
| Jamie | 10 | 14|
| Eric | 9 | 13 |
| Deryk | 7 | 12 |
| Mark | 7 | 11 |
| Matthew | 7 | 10 |
| Neil | 7 | 9 |
| Robert | 6 | 8 |
| Douglas | 5 | 7|
| Steven | 4 | 6 |
| Timothy | 4 | 5 |
| Christopher | 3 | 4 |
| Dana | 3 | 3 |
| George | 2 | 2 |
| James | 1 | 1 |
+-------------+-------+------+

Notice Deryk, Mark, Matthew and Neil for example all have 7 wins -but the $rank variables has incremented regardless (too bad for Neil especially) I want to be able to detect this and stop my $rank variable from incrementing if there is a duplicate elsewhere in the list - such that these 4 guys would all get the rank of 12, yet Robert would still get 8

MY BASIC ALGORITHIM?
Thus in the final version will involve the following:
1) $rank will not be a mutually exclusive list of numbers - duplicates should occur where appropriate, as in the example above.
2) Since Hank_Wins is already sorted in the Select statment, some of the work is already done - I just need to compare the "current"($current) value of $var["Hank_Wins"] with the "previous"($previous) value of $var["Hank_Wins"] as the script moves down the [i]while[i/] loop
3) if $current == $previous then the rank incrementer $rank has to be frozen somehow?
4) once $current does not equal (don't know this syntax) $previous then $rank gets incremented as $rank++

Close?

Okay now, how? - I can't quite get it...how compare values in a while loop? how to freeze the $rank counter for each duplicate "pass"?

Reply With Quote
  #2  
Old July 4th, 2002, 05:48 PM
greggory greggory is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Reims, France
Posts: 82 greggory User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: detecting duplicate values and stalling $rank counter?

This may work better
php Code:
Original - php Code
  1.  
  2. <?php
  3.  
  4. $sql="SELECT Name, Hank_Wins FROM mydatabase ORDER BY Hank_Wins ASC";
  5. $result=MYSQL_QUERY($sql);
  6. $rank=1;
  7.  
  8. // Added
  9. $prevrank=1; $prevhk=0;
  10.  
  11. while($var=MYSQL_FETCH_ARRAY($result)){
  12.   $namemember=$var["Name"];
  13.  
  14.   // Same score twice or more 
  15.   if ($prevhk==$var["Hank_Wins"])
  16.     // Same rank as before
  17.     $hankrankRobert=$prevrank;
  18.   else
  19.   {
  20.     // Remember previous rank
  21.     $prevrank=$rank;
  22.     $hankrankRobert=$rank;
  23.   }
  24.  
  25.     // Remember previous Wins
  26.   $prevhk=$var["Hank_Wins"];
  27.   $rank++;
  28. }
  29. //just for fun an echo for Robert's data
  30. echo $hankrankRobert;
  31.  
  32. ?>

Reply With Quote
  #3  
Old July 4th, 2002, 06:55 PM
mr_fitz mr_fitz is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 13 mr_fitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: detecting duplicate values and stalling $rank counter?

hmmm still not detecting duplicates...

output as follows, if I echo all 14 ranks:
8
14
5
7
9
10
3
6
11
4
13
12
1
2

I see what you are trying to do, I will work with it, if you have further ideas, much appreciated.

Reply With Quote
  #4  
Old July 4th, 2002, 08:44 PM
mr_fitz mr_fitz is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 13 mr_fitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: detecting duplicate values and stalling $rank counter?

Got it working but one other question...

CHANGED CODE
Just had to change one variable in this expression from above:
php Code:
Original - php Code
  1.   if ($prevhk==$var["Hank_Wins"])
  2.     // Same rank as before
  3.     $hankrankRobert=$prevrank;


should read:

php Code:
Original - php Code
  1. if ($prevhk==$var["Hank_Wins"])
  2.     // Same rank as before
  3.    $rank=$prevrank;


THE OUTPUT
6
9
4
5
7
7
3
4
7
3
8
7
1
2
(ordered by surname, so does not look as above)
Which is not exactly what I expected but will be more useful to me in the end since I am using these ranks as weights - this will provide a narrower band of output values - see next.

OUTSTANDING QUESTION:
Excel actually ranks but picking up where it left off after duplicates.
Thus the start rank in the example above would be 14 and the end rank would be 1 - but with duplicates. Thus, if there were duplicates at rank 12 for 3 people it would read 14, 13, 12, 12 ,12, 9, 8,..., 1

I tried implementing this by simply inserting
rank++;

as follows:
php Code:
Original - php Code
  1. if ($prevhk==$var["Hank_Wins"])
  2.     // Same rank as before
  3.     $rank=$prevrank;
  4. rank++;


To increment despite having a duplicate, but this produces the error:
Parse error: parse error, unexpected T_ELSE

How would I increment the $rank counter inside the if before the else - or is this just not possible?

Reply With Quote
  #5  
Old July 5th, 2002, 12:16 AM
mr_fitz mr_fitz is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 13 mr_fitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: detecting duplicate values and stalling $rank counter?

What I have listed above is not right, but the attachment at this thread
http://forums.devshed.com/showthrea...0683#post160683
DOES work properly - see there for what it does.

thanks greggory, got me on the right track.


Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > detecting duplicate values and stalling $rank counter?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway