zero-perfoliate
zero-perfoliate

Author Topic: Displaying Duplicates in a numbered List  (Read 491 times)

Offline patchpony

  • New PHP Members
  • Posts: 1
  • Karma: +0/-0
Displaying Duplicates in a numbered List
« on: March 31, 2010, 08:36:40 AM »
I am wanting to display duplicates in a numbered list but can't seem to figure out what direction to take to get there.

Exampled of what I want (w/ limit of 4 placements to display):

Rank       Name       Pts
------       -------        ----
1.            Pet1         100
2.            Pet2           95
2.            Pet3           95

3.            Pet4           80
4.            Pet5           70

The two in bold are giving me the greef.... currently it shows them as

Rank       Name       Pts
------       -------        ----
1.            Pet1         100
2.            Pet2           95
3.            Pet3           95
4.            Pet4           80
5.            Pet5           70
Pet5 is not shown at all.


Thanks for any assistance!

Offline RoseKnight

  • PHP Help Guru's
  • ****
  • Posts: 52
  • Karma: +0/-0
Re: Displaying Duplicates in a numbered List
« Reply #1 on: March 31, 2010, 05:42:51 PM »
Currently, I don't think that the array sort function in php will do this for you, and you are going to have to make your own sorting function to get it to do what you want.

hotnoob

  • Guest
Re: Displaying Duplicates in a numbered List
« Reply #2 on: March 31, 2010, 06:22:51 PM »
Ahh well if the duplicate is in an array... i wasnt sure if it was from sql or something... well either way using
array_unique() should do the trick.... it doesn't work on multidimensional arrays tho..

Offline RoseKnight

  • PHP Help Guru's
  • ****
  • Posts: 52
  • Karma: +0/-0
Re: Displaying Duplicates in a numbered List
« Reply #3 on: April 01, 2010, 07:18:50 PM »
I got an idea to write code based off of array_unique, but then I stumbled upon multi-sort

Code: [Select]
//using an array formatted like this:
$petlist[0]['name'] = 'pet1';
$petlist[0]['pts'] = 80;
$petlist[1]['name'] = 'pet2';
$petlist[1]['pts'] = 95;
$petlist[2]['name'] = 'pet3';
$petlist[2]['pts'] = 85;
$petlist[3]['name'] = 'pet4';
$petlist[3]['pts'] = 95;
$petlist[4]['name'] = 'pet5';
$petlist[4]['pts'] = 100;

//the below code sorts the array
foreach($petlist as $temp_list) {
    $sort_pts[] = ($temp_list['pts']);
}

array_multisort($sort_pts, SORT_NUMERIC,SORT_DESC, $petlist);

//now we'll make the output for the list:
$out = '';
for($i=0; $i<sizeof($petlist); $i++) {
if(($i>0) && ($petlist[$i]['pts']==$petlist[$i-1]['pts'])) {
//the pet scored the same as the pet above and is in the same rank
$out.=($i).') ';
} else {
//this pet is in the next rank down
$out.=($i+1).') ';
}

$out.=$petlist[$i]['name'].' '.$petlist[$i]['pts']."<br />\n";

}

echo $out;

this will output something like this:
Code: [Select]
1) pet5 100
2) pet2 95
2) pet4 95
4) pet3 85
5) pet1 80

now all you need is some formatting and another variable to change the 4&5 to 3&4.

Offline RoseKnight

  • PHP Help Guru's
  • ****
  • Posts: 52
  • Karma: +0/-0
Re: Displaying Duplicates in a numbered List
« Reply #4 on: April 01, 2010, 07:27:04 PM »
actually the above code also needs to limit it to a maximum of four slots and since I'm bored. I'll fix the code for you so all you have to do is format it.

Code: [Select]
<?php
foreach(
$petlist as $temp_list) {
   
$sort_pts[] = ($temp_list['pts']);
}

array_multisort($sort_ptsSORT_NUMERIC,SORT_DESC$petlist);

$out '';
$rank 0;
$i 0;
while($rank<4) {
if(($i>0) && ($petlist[$i]['pts']==$petlist[$i-1]['pts'])) {
$out.=($rank).') ';
} else {
$out.=($rank+1).') ';
$rank++;
}

$out.=$petlist[$i]['name'].' '.$petlist[$i]['pts']."<br />\n";

$i++;
}

?>


output
Code: [Select]
1) pet5 100
2) pet2 95
2) pet4 95
3) pet3 85
4) pet1 80

now it does everything you want except format it.

I feel like I totally stole the adventure of figuring out that code for yourself now...

 

zero-perfoliate