Jump to content

wadesmart

Members
  • Posts

    7
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

wadesmart's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. While not as streamlined as your code, this went live last night. Thanks for all the help. I appreciate it. <?php $stats = file('./docs/teamstats.txt'); foreach($stats as $line_number => $line){ $l = explode(" ",$line); $newarray[$line_number]["team"] = $l[0]; $newarray[$line_number]["games"] = $l[1]; $newarray[$line_number]["wins"] = $l[2]; $newarray[$line_number]["loses"] = $l[3]; $newarray[$line_number]["ties"] = $l[4]; $newarray[$line_number]["points"] = $l[5]; $newarray[$line_number]["goals"] = $l[6]; } foreach($newarray as $key => $row){ $team[$key] = $row['team']; $games[$key] = $row['games']; $win[$key] = $row['wins']; $loses[$key] = $row['loses']; $ties[$key] = $row['ties']; $points[$key] = $row['points']; $goals[$key] = $row['goals']; } array_multisort($points, SORT_NUMERIC, $goals, SORT_NUMERIC, $newarray); $newarray = array_reverse($newarray); $count = count($newarray); for($i = 0; $i < $count; $i++){ print" <tr> <td>".$newarray[$i]["team"]."</td> <td>".$newarray[$i]["games"]."</td> <td>".$newarray[$i]["wins"]."</td> <td>".$newarray[$i]["loses"]."</td> <td>".$newarray[$i]["ties"]."</td> <td>".$newarray[$i]["points"]."</td> <td>".$newarray[$i]["goals"]."</td> </tr>"; } ?>
  2. Doesnt sort numeric start at 0 and go up? Team Games Wins Loses Ties Points Goals Yellow 1 0 1 0 0 4 Purple 1 0 1 0 0 5 Green 1 0 1 0 0 8 Lt_Blue 1 1 0 0 3 8 White 1 1 0 0 3 9 Dk_Blue 1 1 0 0 3 10 This is what I want but in reverse.
  3. I changed your <> around function sortStats($a, $b) { //Sort by points if($a['points'] != $b['points']) { return ($a['points'] < $b['points']) ? 1 : -1; } //Sort by goals if($a['goals'] != $b['goals']) { return ($a['goals'] > $b['goals']) ? 1 : -1; } //Both points & gaols are the same return 0; } That produces this: Team Games Wins Loses Ties Points Goals Dk_Blue 1 1 0 0 3 10 Lt_Blue 1 1 0 0 3 8 White 1 1 0 0 3 9 Yellow 1 0 1 0 0 4 Purple 1 0 1 0 0 5 Green 1 0 1 0 0 8
  4. Ok. I ran the code you posted with this result: Team Games Wins Loses Ties Points Goals White 1 1 0 0 3 9 Lt_Blue 1 1 0 0 3 8 Dk_Blue 1 1 0 0 3 10 Green 1 0 1 0 0 8 Purple 1 0 1 0 0 5 Yellow 1 0 1 0 0 4
  5. You mean this array_multisort found my code that I posted? array_multisort($points, SORT_DESC, $goals, SORT_DESC, $newarray); Yes I did.
  6. Im looking to sort team stats by points and goals. I thought I was onto something but Ive hit a spot I get around. Below Ive posted the file I pull from, the code and two sets of results. My intended end result would show: Dk Blue 1 1 0 0 3 10 White 1 1 0 0 3 9 Lt Blue 1 1 0 0 3 8 Green 1 0 1 0 0 8 Purple 1 0 1 0 0 5 Yellow 1 0 1 0 0 4 teamstats.txt: Dk_Blue 1 1 0 0 3 10 Green 1 0 1 0 0 8 Lt_Blue 1 1 0 0 3 8 Purple 1 0 1 0 0 5 White 1 1 0 0 3 9 Yellow 1 0 1 0 0 4 <?php $stats = file('teamstats.txt'); foreach($stats as $line_number => $line){ $l = explode(" ",$line); $newarray[$line_number]["team"] = $l[0]; $newarray[$line_number]["games"] = $l[1]; $newarray[$line_number]["wins"] = $l[2]; $newarray[$line_number]["loses"] = $l[3]; $newarray[$line_number]["ties"] = $l[4]; $newarray[$line_number]["points"] = $l[5]; $newarray[$line_number]["goals"] = $l[6]; } foreach($newarray as $key => $row){ $team[$key] = $row['team']; $games[$key] = $row['games']; $win[$key] = $row['wins']; $loses[$key] = $row['loses']; $ties[$key] = $row['ties']; $points[$key] = $row['points']; $goals[$key] = $row['goals']; } array_multisort($points, SORT_DESC, $goals, SORT_DESC, $newarray); $count = count($newarray); print"<table> <tr> <th>Team</th> <th>Games</th> <th>Wins</th> <th>Loses</th> <th>Ties</th> <th>Points</th> <th>Goals</th> </tr>"; for($i = 0; $i < $count; $i++){ print" <tr> <td>".$newarray[$i]["team"]."</td> <td>".$newarray[$i]["games"]."</td> <td>".$newarray[$i]["wins"]."</td> <td>".$newarray[$i]["loses"]."</td> <td>".$newarray[$i]["ties"]."</td> <td>".$newarray[$i]["points"]."</td> <td>".$newarray[$i]["goals"]."</td> </tr>"; } print"</table>"; ?> Results: When I run ONLY Team, Points & Goals sorting on points and goals I have this result: Team Points Goals Dk Blue 3 10 White 3 9 Lt Blue 3 8 Green 0 8 Purple 0 5 Yellow 0 4 When I sort on all as in the code above but still sorting only points and goals I have this result: Team Games Wins Loses Ties Points Goals White 1 1 0 0 3 9 Lt_Blue 1 1 0 0 3 8 Dk_Blue 1 1 0 0 3 10 Green 1 0 1 0 0 8 Purple 1 0 1 0 0 5 Yellow 1 0 1 0 0 4 I found a function on http://www.the-art-of-web.com/php/sortarray/ that used this code <?PHP function compare_fullname($a, $b) { $retval = strnatcmp($a['lastname'], $b['lastname']); if(!$retval) return strnatcmp($a['firstname'], $b['firstname']); return $retval; } // sort alphabetically by firstname and lastname usort($data, 'compare_fullname'); ?> to do something similar on names but, the problem with usort which I found out very quickly is, "If two members compare as equal, their relative order in the sorted array is undefined."- php.net Any suggestion as to how to get these in the order I seek? Thanks Wade
×
×
  • 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.