amlxv

index.php

Aug 1st, 2022 (edited)
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.14 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * CABARAN JOMLAUNCH YOUTH 2022 (WEB) 02
  5.  *
  6.  * Nama Peserta: Amirul
  7.  * Telegram: @amlxv
  8.  *
  9.  * CSS Framework: TailwindCSS
  10.  * Font Source: Google Fonts
  11.  * Font: Poppins
  12.  *
  13.  * @see https://i.imgur.com/5HMyNBV.png
  14.  *
  15.  */
  16.  
  17. include("random_data.php");
  18.  
  19. /**
  20.  * This function is used to filter
  21.  * the people by their gender.
  22.  *
  23.  */
  24. function filterPeopleByGender($people, $gender)
  25. {
  26.     return array_filter(
  27.         $people,
  28.         fn ($person) => $person["Gender"] == $gender
  29.     );
  30. }
  31.  
  32. /**
  33.  * This function is used to handle
  34.  * the table header.
  35.  *
  36.  */
  37. function handleTableHeader()
  38. {
  39.     $class = "px-4 py-3.5 text-left text-sm font-semibold text-gray-900";
  40.     $title = array("ID", "FIRST NAME", "LAST NAME", "AGE", "YEARS");
  41.     echo "<tr>" .
  42.         join(
  43.             "",
  44.             array_map(
  45.                 fn ($title) => "<th scope='col' class=\"$class\">$title</th>",
  46.                 $title
  47.             )
  48.         ) .
  49.         "</tr>";
  50. }
  51.  
  52. /**
  53.  * This function is used to handle
  54.  * the table body.
  55.  *
  56.  */
  57. function handleTableBody($people, $gender)
  58. {
  59.     $class = "px-6 py-4 text-sm text-gray-900";
  60.  
  61.     foreach ($people as $key => $person) {
  62.         if ($key == array_key_first($people))
  63.             echo "<tr><td colspan='5' class=\"$class text-center bg-green-200 border border-green-400 font-semibold\">"
  64.                 . strtoupper($gender)
  65.                 . "</td></tr>";
  66.  
  67.         echo "<tr>
  68.                <td class=\"$class\">$key</td>
  69.                <td class=\"$class\">{$person['First Name']}</td>
  70.                <td class=\"$class\">{$person['Last Name']}</td>
  71.                <td class=\"$class\">{$person['Age']}</td>
  72.                <td class=\"$class\">{$person['Years']}</td>
  73.             </tr>";
  74.     }
  75. }
  76.  
  77. /**
  78.  * Seperate the people
  79.  * by their gender type
  80.  *
  81.  */
  82. $people = array(
  83.     "female" => filterPeopleByGender($data, "Female"),
  84.     "male" => filterPeopleByGender($data, "Male"),
  85. );
  86.  
  87. ?>
  88.  
  89. <!-- Start working with displaying the table on the browser  -->
  90.  
  91. <head>
  92.     <link rel="preconnect" href="https://fonts.googleapis.com">
  93.     <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  94.     <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
  95.     <script src="https://cdn.tailwindcss.com"></script>
  96.     <script>
  97.     tailwind.config = {
  98.         theme: {
  99.             fontFamily: {
  100.                 sans: ["Poppins", "sans-serif"],
  101.             },
  102.         }
  103.     }
  104.     </script>
  105. </head>
  106.  
  107. <body class="p-4 bg-gradient-to-r from-green-300 via-blue-500 to-purple-600">
  108.     <div class="px-4 sm:px-6 lg:px-8">
  109.         <div class="mt-8 flex flex-col items-center">
  110.             <div class="-my-2 -mx-4 overflow-x-auto sm:-mx-6 lg:-mx-8">
  111.                 <div class="inline-block min-w-fit py-2 align-middle md:px-6 lg:px-8">
  112.                     <div class="overflow-hidden rounded-lg">
  113.                         <table class="min-w-fit">
  114.                             <thead class="bg-green-50">
  115.                                 <?php
  116.                                 /**
  117.                                  * Call the handleTableHeader function
  118.                                  * to display the table header.
  119.                                  *
  120.                                  */
  121.                                 handleTableHeader();
  122.                                 ?>
  123.                             </thead>
  124.                             <tbody class=" bg-green-50">
  125.                                 <?php
  126.                                 /**
  127.                                  * Iterate over the people array and
  128.                                  * display the table body.
  129.                                  *
  130.                                  */
  131.                                 foreach ($people as $gender => $people) {
  132.                                     handleTableBody($people, $gender);
  133.                                 }
  134.                                 ?>
  135.                             </tbody>
  136.                         </table>
  137.                     </div>
  138.                 </div>
  139.             </div>
  140.         </div>
  141.     </div>
  142. </body>
Advertisement
Add Comment
Please, Sign In to add comment