Advertisement
Guest User

Untitled

a guest
Dec 8th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.18 KB | None | 0 0
  1. <html>
  2.     <head>
  3.         <title>Student Sorting</title>
  4.     </head>
  5.     <body>
  6.         <?php
  7.         if (isset($_POST['firstNames']) && isset($_POST['lastNames']) && isset($_POST['emails']) &&
  8.             isset($_POST['scores'])) {
  9.  
  10.             $firstNames = $_POST['firstNames'];
  11.             $lastNames = $_POST['lastNames'];
  12.             $emails = $_POST['emails'];
  13.             $scores = $_POST['scores'];
  14.             $sortChoice = $_POST['sort'];
  15.             $order = $_POST['order'];
  16.  
  17.             $data = [];
  18.             for ($index = 0; $index < count($firstNames); $index++) {
  19.                 $firstName = htmlspecialchars($firstNames[$index]);
  20.                 $lastName = htmlspecialchars($lastNames[$index]);
  21.                 $email = htmlspecialchars($emails[$index]);
  22.                 $score = htmlspecialchars($scores[$index]);
  23.                 $data[] = new stdClass();
  24.  
  25.                 $data[count($data) - 1]->firstName = $firstName;
  26.                 $data[count($data) - 1]->lastName = $lastName;
  27.                 $data[count($data) - 1]->email = $email;
  28.                 $data[count($data) - 1]->score = $score;
  29.             }
  30.  
  31.             $data = sortByChoice($sortChoice, $order, $data);
  32.  
  33.             var_dump($data);
  34.  
  35.  
  36. //            echo '<table>' . "\n" .
  37. //                '<tr><th>First Name</th><th>Last Name</th><th>Email</th><th>Exam Score</th></tr>';
  38. //
  39. //            for ($index = 0; $index < count($firstNames); $index++) {
  40. //                $firstName = htmlspecialchars($firstNames[$index]);
  41. //                $lastName = htmlspecialchars($lastNames[$index]);
  42. //                $email = htmlspecialchars($emails[$index]);
  43. //                $score = htmlspecialchars($scores[$index]);
  44. //
  45. //                echo "<tr><td>$firstName</td><td>$lastName</td><td>$email</td><td>$score</td></tr>";
  46. //            }
  47. //
  48. //            echo '</table>';
  49.  
  50.         }
  51.         ?>
  52.  
  53.         <?php
  54.         function sortByChoice($property, $order, $data) {
  55.             usort($data, function($a, $b, $property)
  56.             {
  57.                 return strcmp($a->$property, $b->$property);
  58.             });
  59.  
  60.             return $data;
  61.         }
  62.         ?>
  63.     </body>
  64. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement