Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>My Bills</title>
  4. </head>
  5. <body>
  6. <h1>My Bills</h1>
  7. <form method="post">
  8. <h2>Sort Order for Items:
  9.  
  10. <?php
  11.   $sort_order = $_POST['sort_order'];
  12.  
  13.   $target_string = $_POST['targetstring'];
  14.   $targe_string = trim($target_string);
  15.  
  16.   $total = 0;
  17.   $error_cnt = 0;
  18.  
  19.   echo "<span id=\"order\">Ascending <input type=radio name=sort_order value=ascending checked>&nbsp;&nbsp;&nbsp;\n";
  20.   echo "Descending <input type=radio name=sort_order value=descending>&nbsp;&nbsp;&nbsp;</span></h2>\n";
  21.  
  22.  
  23.   $bills_array = array();
  24.  
  25.  
  26.   for ($i = 1; $i < 5; $i++) {
  27.       $item_name = 'item_name' . $i;
  28.       $item_value = $_POST[$item_name];
  29.       $amount_name = 'amount_name' . $i;
  30.       $amount_value = $_POST[$amount_name];
  31.       $amount_value = trim($amount_value);
  32.       $bills_element = "$item_value*$amount_value*";
  33.       array_push($bills_array, $bills_element);
  34.   }
  35.  
  36.   if ($sort_order == 'descending') {
  37.       rsort($bills_array);
  38.   } else {
  39.       sort($bills_array);
  40.   }
  41. ?>
  42.  
  43. <br /><b>Find items that contain:</b>
  44. <input type="text" name="targetstring" size="20"><br /><br />
  45.  
  46. <table>
  47.  
  48. <tr>
  49. <th>Item</th>
  50. <th>Amount</th>
  51. </tr>
  52.  
  53. <?php
  54.   for ($i = 1; $i < 5; $i++) {
  55.       $item_name = 'item_name' . $i;
  56.       $item_value = $_POST[$item_name];
  57.       $amount_name = 'amount_name' . $i;
  58.       $amount_value = $_POST[$amount_name];
  59.       $amount_value = trim($amount_value);
  60.      
  61.       $array_counter = $i - 1;
  62.       $item_value_from_array = $item_value;
  63.       $item_value_from_array = $bills_array[$array_counter];
  64.       list($item_value_from_array, $amount_value) = explode("*", $bills_array[$array_counter]);
  65.      
  66.       echo "<tr>\n";
  67.      
  68.       $style = "";
  69.       if (!empty($target_string)) {
  70.         $pos = strpos($item_value_from_array, $target_string);
  71.         if ($pos !== false) {
  72.           $style = ' style="background-color: Yellow;"';
  73.         }
  74.       }
  75.       echo "<td><input type=text name=" . $item_name . " value=" . $item_value_from_array . "$style></td>\n";
  76.       echo "<td><input type=text name=" . $amount_name . " value=" . $amount_value . "$style></td>\n";
  77.  
  78.       if (!empty($amount_value)) {
  79.           if (is_numeric($amount_value)) {
  80.               $total = $total + $amount_value;
  81.           } else {
  82.               echo "<td><font color=red> ERROR: " . $amount_value . " is not a number.</font></td>\n";
  83.               $error_cnt++;
  84.           }
  85.       }
  86.      
  87.      
  88.       echo "</tr>\n";
  89.   }
  90. ?>
  91.  
  92. </table>
  93.  
  94. <?php
  95.   if ($error_cnt > 0) {
  96.       echo "<font color=red><p>Errors: " . $error_cnt . "</font></p>\n";
  97.   } else {
  98.       echo "<font color=green><p>Total Amount For All Bills: $total</font></p>\n";
  99.   }
  100. ?>
  101.  
  102. <br><input type="submit" value="Submit">
  103. <br />
  104.  
  105.  
  106. </form>
  107. </body>
  108. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement