stb74 Posted August 10, 2007 Share Posted August 10, 2007 Hi I am trying to create a function that will enable me to sort a soccer league table. Sort will be on several fields Points GoalDiff GoalsFor I found this example that I though might help me find a solution but it gives an error Parse error: parse error, unexpected T_NEW in D:\XAMPP Web\xampp\htdocs\nifootball\sort.php on line 29 function sortDataSet(&$dataSet) { $args = func_get_args(); $callString = 'array_multisort('; $usedColumns = array(); for($i = 1, $count = count($args); $i < $count; ++$i) { switch(gettype($args[$i])) { case 'string': $callString .= '$dataSet[\''.$args[$i].'\'], '; array_push($usedColumns, $args[$i]); break; case 'integer': $callString .= $args[$i].', '; break; default: throw new Exception('expected string or integer, given '.gettype($args[$i])); } } foreach($dataSet as $column => $array) { if(in_array($column, $usedColumns)) continue; $callString .= '$dataSet[\''.$column.'\'], '; } eval(substr($callString, 0, -2).');'); } /* example usage */ /* example usage */ $latestPost = array( 'team' => array( '0', '1', '2', '4', '5' ), 'name' => array( 'team1', 'team2', 'team3', 'team4', 'team5' ), 'date' => array( '10', '8', '12', '20', '4' ) ); sortDataSet($latestPost, 'name', SORT_DESC, SORT_STRING, 'team', SORT_DESC, SORT_STRING); echo '<pre>'; print_r($latestPost); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/64254-sorting-in-php/ Share on other sites More sharing options...
gurroa Posted August 11, 2007 Share Posted August 11, 2007 Perhaps your are not running PHP 5.X and newer. See http://www.php.net/exceptions Quote Link to comment https://forums.phpfreaks.com/topic/64254-sorting-in-php/#findComment-320922 Share on other sites More sharing options...
cooldude832 Posted August 11, 2007 Share Posted August 11, 2007 Its easier than you think. The trick is this 1) Execute a query 2) In a while loop make a multi dimensional array of said data i.e ($data[$i]['Name'] $data[$i]['Stats']) 3) In same while loop create a single level array of each stat you wish to sort by i.e ($name = array(); $name[$i] = $row['Name']) 4) User sort or reverse sort to get your sorting parms in correct order. 5) Then apply sorting from a top down method (i.e first sorted will be last one in order) The sorting is the tricky part, but php has a function for it. its called multi sort read: http://us2.php.net/manual/en/function.array-multisort.php and you should understand it pretty easily. once you done that say print_r($data) and then view the source to see the array sorted. Example 261 on that page explains it very well Quote Link to comment https://forums.phpfreaks.com/topic/64254-sorting-in-php/#findComment-320925 Share on other sites More sharing options...
stb74 Posted August 11, 2007 Author Share Posted August 11, 2007 I as able to get it working by this. function sortTable($array) { foreach ($array as $key => $row) { $Points[$key] = $row['Points']; $GoalDiff[$key] = $row['GoalDiff']; $GoalsFor[$key] = $row['GoalsFor']; $teamName[$key] = $row['teamName']; } array_multisort($Points, SORT_DESC, $GoalDiff, SORT_DESC, $GoalsFor, SORT_DESC, $teamName, SORT_STRING, $array); return $array; } Is this way ok. Quote Link to comment https://forums.phpfreaks.com/topic/64254-sorting-in-php/#findComment-320970 Share on other sites More sharing options...
Guest Posted August 11, 2007 Share Posted August 11, 2007 It was as gurroa said, you are not running PHP 5.x.x (in fact, you are probably running PHP 4). You may need to request an upgrade (if its not your server). That way the first script will work. Quote Link to comment https://forums.phpfreaks.com/topic/64254-sorting-in-php/#findComment-320983 Share on other sites More sharing options...
stb74 Posted August 11, 2007 Author Share Posted August 11, 2007 Doh My server copy is running 5 but for some stupid reason my xammp was running 4. Quote Link to comment https://forums.phpfreaks.com/topic/64254-sorting-in-php/#findComment-320986 Share on other sites More sharing options...
Barand Posted August 11, 2007 Share Posted August 11, 2007 try <?php $league = array ( 'team1' => array ( 'Points' => 8, 'GoalDiff' => 3, 'GoalsFor' => 4 ), 'team2' => array ( 'Points' => 10, 'GoalDiff' => 5, 'GoalsFor' => 8 ), 'team3' => array ( 'Points' => 10, 'GoalDiff' => 6, 'GoalsFor' => 8 ), 'team4' => array ( 'Points' => 30, 'GoalDiff' => 9, 'GoalsFor' => 12 ), 'team5' => array ( 'Points' => 10, 'GoalDiff' => 6, 'GoalsFor' => 9 ) ); uasort ($league, 'leaguesort'); // custom sort echo '<pre>', print_r($league, true), '</pre>'; // view result function leaguesort ($a, $b) { if ($a['Points'] == $b['Points']) { // sort by goaldiff if ($a['GoalDiff'] == $b['GoalDiff']) { // sort by goalsfor if ($a['GoalsFor'] == $b['GoalsFor']) return 0; return $a['GoalsFor'] > $b['GoalsFor'] ? -1 : 1; } return $a['GoalDiff'] > $b['GoalDiff'] ? -1 : 1; } return $a['Points'] > $b['Points'] ? -1 : 1; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64254-sorting-in-php/#findComment-321045 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.