Advertisement
Guest User

SoftUni students collection sort

a guest
Sep 1st, 2014
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  1. <?php
  2. $students = explode("\n", $_GET['students']);
  3.  
  4. $id = 1;
  5. $studentsCollection = [];
  6. foreach ($students as $student) {
  7.     if (empty($student)) continue;
  8.     $student = explode(", ", $student);
  9.  
  10.     $studentsCollection[] = array(
  11.         'Id' => $id++,
  12.         'Username' => htmlspecialchars(($student[0])),
  13.         'Email' => htmlspecialchars(($student[1])),
  14.         'Type' => htmlspecialchars(($student[2])),
  15.         'Result' => htmlspecialchars(trim($student[3]))
  16.     );
  17. }
  18.  
  19. foreach ($studentsCollection as $key => $val) {
  20.     $ids[$key] = $val['Id'];
  21.     $column[$key] = $val[ucfirst($_GET['column'])];
  22. }
  23.  
  24. $order = strtolower($_GET['order']) == 'descending' ? SORT_DESC : SORT_ASC;
  25. array_multisort($column, $order, $ids, $order, $studentsCollection);
  26.  
  27.  
  28. echo "<table><thead><tr><th>Id</th><th>Username</th><th>Email</th><th>Type</th><th>Result</th></tr></thead>";
  29. foreach ($studentsCollection as $row) {
  30.     echo "<tr><td>{$row['Id']}</td><td>{$row['Username']}</td><td>{$row['Email']}</td><td>{$row['Type']}</td><td>{$row['Result']}</td></tr>";
  31. }
  32. echo "</table>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement