By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

How do I search a string within a text input box? (String Manipulation)

P: 2
Hi everyone,

I currently have an assignment which I am almost done with but am having problems trying to figure out how to integrate a string function search. I have a text box called $targetstring. My program allows users to input items such as groceries into one text box (item_value) and add an amount to the groceries purchased (amount_value).

The program will add the amounts and give a total and will validate the amount_value box for inputs that are not numerical and give an error count if anything other than a number is entered.

I have also added to radio buttons that let you sort the array of values into ascending or descending order.

Everything works fine but I am trying to add one other feature. A text input box called $targetstring that will allow a user to input any text, be it a phrase, letters or single letter and then the program will search all the values input into the item_value text boxes and will highlight them yellow if they match any of the variables entered into the $targetstring input box.

I have included my code. This last part of the assignment is driving me crazy so any help would be greatly appreciated.

Thanks,
Mike


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Assignment </title>
  4. </head>
  5.  
  6. <body>
  7.  
  8. <body style="font-family: Arial, Helvetica, sans-serif; color: black;">
  9.  
  10. <h1>My Bills</h1>
  11.  
  12.  
  13. <form method=post>
  14.  
  15. <br><b>Sort order for items:</b>
  16. <br>
  17. <br> 
  18.  
  19. <?php
  20.  
  21.     $sort_order = $_POST['sort_order']; //added code for asgn 6
  22.  
  23.     $targetstring = $_POST['targetstring'];  //added code for asgn 7
  24.  
  25.  
  26.     if (isset($_POST['Submit'])) //The following code is intended to retain the choice of the radio button chosen by the user
  27.  
  28.  
  29. $sort_order = $_POST['sort_order'];
  30.  
  31.     switch($sort_order)
  32.     {
  33.         case 'desc' :
  34.             print "Ascending <input type=radio name=sort_order value=asc>&nbsp;&nbsp;&nbsp;";
  35.             print "Descending <input type=radio name=sort_order value=desc checked>&nbsp;&nbsp;&nbsp;";
  36.             break;
  37.  
  38.         default :
  39.  
  40.             print "Ascending <input type=radio name=sort_order value=asc checked>&nbsp;&nbsp;&nbsp;";
  41.             print "Descending <input type=radio name=sort_order value=desc>&nbsp;&nbsp;&nbsp;";
  42.             break;
  43.  
  44.     }
  45.  
  46.  
  47.     $sort_line = array();  //Declare an empty array - added code for asgn 6
  48.  
  49.  
  50. for ($row = 1; $row <5 ; $row++) // creates rows that are less than 5
  51. {
  52.  
  53.     $item_name='item'.$row; // assign values
  54.     $item_value=$_POST[$item_name]; // assign values
  55.  
  56.     $amount_name='amount'.$row;  // assign values
  57.     $amount_value=$_POST[$amount_name]; // assign values
  58.  
  59.  
  60.     $myelement = "$item_value*$amount_value*";
  61.  
  62.  
  63.     array_push($sort_line, $myelement); //Adds $myelement to the end of the $sort_line array
  64.  
  65. }    
  66.  
  67.     if ($sort_order == 'desc')
  68.     {
  69.     rsort($sort_line);  //Sorts array in place - added code for asgn 6
  70.     } else {
  71.     sort($sort_line);  //Sorts array in place - added code for asgn 6
  72.            }
  73.  
  74.  
  75. ?>
  76.  
  77.  
  78. <br />
  79. <br />
  80.  
  81.  
  82. <br><b>Find Items that Contain:</b>
  83. <input type=text name=targetstring size=20>
  84. <br>
  85. <br>
  86.  
  87.  
  88. <table>
  89.  
  90. <tr>
  91. <th>Item</th><th>Amount</th>
  92. </tr>
  93.  
  94.  
  95. <?php
  96.  
  97.  
  98. $err_cnt = 0;
  99. $total_amt = 0;
  100. $read_ctr = 1;    
  101.  
  102.  
  103. for ($row = 1; $row <5 ; $row++) // creates rows that are less than 5
  104.  
  105. {        
  106.     $item_name='item'.$row; // assign values
  107.  
  108.     $array_counter = $row - 1;  //To make sure to get element 0 in the array - added code for asgn 6
  109.  
  110.  
  111.     $amount_name='amount'.$row;  // assign values
  112.  
  113.     $array_counter = $row - 1;  //To make sure to get element 0 in the array - added code for asgn 6
  114.  
  115.  
  116.     list($item_value_from_array, $amount_value_from_array) = explode('*',$sort_line[$array_counter]);
  117.  
  118.  
  119.     print"<tr>\n";
  120.     print"<td><input type=text name=".$item_name." value='$item_value_from_array'></td>\n"; // prints rows with saved values - added code for asgn 6
  121.     print"<td><input type=text name=".$amount_name." value='$amount_value_from_array'></td>\n"; // prints rows with saved values - added code for asgn 6
  122.  
  123.  
  124.     list($item_read, $amount_read) = explode("*", $sort_line[$array_counter]); // attempting to break up the Array elements into variables using explode function
  125.  
  126.  
  127.  
  128. if (!empty($amount_read)) //if amount value is not empty
  129.     {
  130.         if (is_numeric($amount_read)) // checks to see if amount value is a numeric number
  131.         {
  132.             $total = $total + $amount_read; //adds the total of the amount column and created the $total variable
  133.         } else {
  134.             print"<td><font color=red>Amount: $amount_read is not a number</font></td>"; // if amount value is not numberic then prints error in red
  135.             $error_count++; // counts number of invalid amounts
  136.         }
  137.     }
  138.  
  139.     print"</tr>\n";
  140. }
  141.  
  142. ?>
  143.  
  144.  
  145. </table>
  146.  
  147.  
  148. <?php
  149.     if ($error_count >0) // if statement checking to see if errors; if there are, then prints them. If not then prints total amount only
  150.     {
  151.         print"<br>Errors: $error_count"; // prints total of invalid amount errors
  152.     } else {
  153.         print"<br>Total: $total"; // prints total of amounts
  154.     }
  155. ?>
  156.  
  157.  
  158. <br><br><input type=submit value=Submit>
  159. <br>
  160. <br>
  161.  
  162.  
  163. </form>
  164. </body>
  165. </html>
Apr 14 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.