Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>Student Sorting</title>
- </head>
- <body>
- <?php
- if (isset($_POST['firstNames']) && isset($_POST['lastNames']) && isset($_POST['emails']) &&
- isset($_POST['scores'])) {
- $firstNames = $_POST['firstNames'];
- $lastNames = $_POST['lastNames'];
- $emails = $_POST['emails'];
- $scores = $_POST['scores'];
- $sortChoice = $_POST['sort'];
- $order = $_POST['order'];
- $data = [];
- for ($index = 0; $index < count($firstNames); $index++) {
- $firstName = htmlspecialchars($firstNames[$index]);
- $lastName = htmlspecialchars($lastNames[$index]);
- $email = htmlspecialchars($emails[$index]);
- $score = htmlspecialchars($scores[$index]);
- $data[] = new stdClass();
- $data[count($data) - 1]->firstName = $firstName;
- $data[count($data) - 1]->lastName = $lastName;
- $data[count($data) - 1]->email = $email;
- $data[count($data) - 1]->score = $score;
- }
- $data = sortByChoice($sortChoice, $order, $data);
- var_dump($data);
- // echo '<table>' . "\n" .
- // '<tr><th>First Name</th><th>Last Name</th><th>Email</th><th>Exam Score</th></tr>';
- //
- // for ($index = 0; $index < count($firstNames); $index++) {
- // $firstName = htmlspecialchars($firstNames[$index]);
- // $lastName = htmlspecialchars($lastNames[$index]);
- // $email = htmlspecialchars($emails[$index]);
- // $score = htmlspecialchars($scores[$index]);
- //
- // echo "<tr><td>$firstName</td><td>$lastName</td><td>$email</td><td>$score</td></tr>";
- // }
- //
- // echo '</table>';
- }
- ?>
- <?php
- function sortByChoice($property, $order, $data) {
- usort($data, function($a, $b, $property)
- {
- return strcmp($a->$property, $b->$property);
- });
- return $data;
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement