473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

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
0 1798

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

Similar topics

1
by: Edwin Parker | last post by:
I'm fairly new to C, but have most of the basics covered. I have a file that my program writes to, but I need a way to search for information within that file and then edit that information or...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
3
by: Russell | last post by:
Hey, ok i have numerous tables to search through for a 'site search'. some of the searchble fields have html embeded within so after some quick referencing, saw I can use the regExp function...
1
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
1
by: cglewis03 | last post by:
Hello, I am trying to build a search form with several different options to choose from. Currently it is set up to open within the same window if a single option is selected and open within a...
1
by: nganglove | last post by:
C++ string search -------------------------------------------------------------------------------- Hello, please can any one help me? I am given an assigment in C++ to read a text file and...
1
by: amiparna | last post by:
There is two frame in my form.In d first frame there is a text box,and i give some word in the text box.I want to search the word from a file from the location(http://45.12.09.31...........) and if...
41
by: nik707 | last post by:
Hello all, First of all my name is Shan and I am currently learning and also designing a database in Access. Your forum users seems to be very helpful and experts in this matter so I thought I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.