Advertisement
dimipan80

Rich People’s Problems

Apr 22nd, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. <!--You are a very rich billionaire with an unhidden passion for cars. You like certain car manufacturers but you don’t really care about anything else, and that’s why you need your own randomizing algorithm that helps you decide how many and what color cars you should buy. Write a PHP script CarRandomizer.php that receives a string of cars from an input HTML form, separated by a comma and space (“, “). It then prints each car, a random color and a random quantity in a table like the one shown below. Use colors by your choice. Use as quantity a random number in range [1...5]. Styling the page is optional.-->
  2.  
  3. <!DOCTYPE html>
  4. <html>
  5. <head lang="en">
  6.     <meta charset="UTF-8"/>
  7.     <title>Car Randomizer</title>
  8.     <style type="text/css">
  9.         body {
  10.             width: 480px;
  11.             height: 500px;
  12.         }
  13.  
  14.         table {
  15.             margin: auto;
  16.         }
  17.  
  18.         table, th, td {
  19.             border: 1px solid #000;
  20.         }
  21.     </style>
  22. </head>
  23. <body>
  24. <form method="post">
  25.     <p>
  26.         <label for="text-cars">Enter cars</label>
  27.         <input type="text" name="cars" id="text-cars" required/>
  28.         <input type="submit" value="Show result"/>
  29.     </p>
  30. </form>
  31. <table>
  32.     <thead>
  33.     <tr>
  34.         <th>Car</th>
  35.         <th>Color</th>
  36.         <th>Count</th>
  37.     </tr>
  38.     </thead>
  39.     <tbody>
  40.     <?php
  41.     if (!array_key_exists('cars', $_POST) || strlen(trim($_POST['cars'])) === 0) {
  42.         die('The Form can\'t been Empty!!!');
  43.     }
  44.  
  45.     $_POST['cars'] = trim($_POST['cars']);
  46.     if (isset($_POST['cars'])) {
  47.         $cars = preg_split("/[,\s]+/", $_POST['cars'], -1, PREG_SPLIT_NO_EMPTY);
  48.         $colors = array('black', 'blue', 'brown', 'gray', 'green', 'orange', 'red', 'silver',
  49.             'white', 'yellow');
  50.         foreach ($cars as $car) {
  51.             ?>
  52.             <tr>
  53.                 <td><?= htmlspecialchars($car) ?></td>
  54.                 <td><?= $colors[rand(0, 9)] ?></td>
  55.                 <td><?= rand(1, 5) ?></td>
  56.             </tr>
  57.         <?php
  58.         }
  59.     }
  60.     ?>
  61.     </tbody>
  62. </table>
  63. </body>
  64. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement