473,511 Members | 11,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String Manipulation - Textbox searching question

2 New Member
Hello,

I have been having problems with some code I am writing and have not been able to figure out what I am doing wrong, even after some debugging.

I have a textbox that users can input any letters into, that should should search the item field boxes and highlight any matching strings. If a matching search is made, then the item box should remain yellow, and the other item boxes should be white.

It does not have to be a perfect match. Any letter that matches, such as the letter '"j" in the word "juice" should be enough to make the item field box, highlight yellow.

I am including my code but have not been able to figure out how to use this code:

str_ireplace($targetstring, '<span style="background-color:#ff0000;">' . $targetstring . '</span>', $item_value);

(I must be doing something wrong) Any help with this would be greatly appreciated.

Thanks.





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

There are two things that you need to fix to get this working.

First off, the line you posted first, line #129 in the code, it does absolutely nothing. The function, str_ireplace, replaces a string and returns the result. Calling it without actually capturing the return value is pointless. - Also, in this case it is the wrong function to use anyways, so you may as well remove it entirely.

What you can do to make this work is modify the boolean expression on line #133. You got the right idea: to compare the test string with the value of the current text box, but you need to do more than see if they are equal. You need to search within the value to see if it contains the test string. - To do that, you can use the stripos function. If there is a match, it will return the index where the match occurred, if not it returns FALSE. So we can use that to search one string with another like so:
Expand|Select|Wrap|Line Numbers
  1. if(stripos($string_to_search, $search_phrase) !== false) {
  2.     echo "They match!!";
  3. }
  4. else {
  5.     echo "Sorry, no match!";
  6. }
Also, make sure you use the right value. That is: the $item_value_from_array variable, not the $item_value variable.
May 3 '10 #2

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

Similar topics

18
2488
by: jblazi | last post by:
I should like to search certain characters in a string and when they are found, I want to replace other characters in other strings that are at the same position (for a very simply mastermind game)...
9
2378
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
7
11580
by: John A Grandy | last post by:
what are the preferred VB.NET analogues for IsNumeric() and Len() and CInt() & similar string-manipulation functions in VB6
4
3471
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
5
7457
by: Niyazi | last post by:
Hi, Does anyone knows any good code for string manipulation similar to RegularExpresion? I might get a value as string in a different format. Example: 20/02/2006 or 20,02,2006 or ...
4
1473
by: yumyu | last post by:
Hi, If i have a Title called " John Smith" and if i press a command button, it changes to "Smith nhoJ" How do i do that with loop and string manipulation? Thanks
3
1547
by: crprajan | last post by:
String Manipulation: Given a string like “This is a string”, I want to remove all single characters( alphabets and numerals) like (a, b, 1, 2, .. ) . So the output of the string will be “This is...
4
9527
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char...
3
3542
by: frankeljw | last post by:
I have 2 Java strings 1st String is a series of names, colons, and numbers ie) Name1:13:Name2:4526:Name3:789:Name4:3729:Name5:6:Name6:44 2nd String is a name ie) Name2 I need to get the...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7430
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...
1
7089
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
7517
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
5673
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,...
1
5072
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...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.