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($t argetstring, '<span style="backgrou nd-color:#ff0000;" >' . $targetstring . '</span>', $item_value);
(I must be doing something wrong) Any help with this would be greatly appreciated.
Thanks. - <html>
-
-
-
<head>
-
-
<title>testcode</title>
-
-
</head>
-
-
-
<body>
-
-
<body style="font-family: Arial, Helvetica, sans-serif; color: black;">
-
-
<h1>Test</h1>
-
-
-
<form method=post>
-
-
-
<br><b>Sort order for items:</b>
-
<br>
-
<br>
-
-
-
<?php
-
-
$sort_order = trim($_POST['sort_order']);
-
-
$targetstring = trim($_POST['targetstring']);
-
-
-
if (isset($_POST['Submit'])) //The following code is intended to retain the choice of the radio button chosen by the user
-
-
-
$sort_order = $_POST['sort_order'];
-
-
switch($sort_order)
-
{
-
case 'desc' :
-
print "Ascending <input type=radio name=sort_order value=asc> ";
-
print "Descending <input type=radio name=sort_order value=desc checked> ";
-
break;
-
-
default :
-
-
print "Ascending <input type=radio name=sort_order value=asc checked> ";
-
print "Descending <input type=radio name=sort_order value=desc> ";
-
break;
-
-
}
-
-
$sort_line = array(); //Declare an empty array
-
-
for ($row = 1; $row <5 ; $row++) // creates rows that are less than 5
-
{
-
-
$item_name='item'.$row; // assign values
-
$item_value=$_POST[$item_name]; // assign values
-
-
$amount_name='amount'.$row; // assign values
-
$amount_value=$_POST[$amount_name]; // assign values
-
-
-
$myelement = "$item_value*$amount_value*";
-
-
-
array_push($sort_line, $myelement); //Adds $myelement to the end of the $sort_line array
-
-
-
}
-
-
if ($sort_order == 'desc')
-
{
-
rsort($sort_line); //Sorts array in place
-
} else {
-
sort($sort_line); //Sorts array in place
-
}
-
-
-
?>
-
-
-
<br />
-
<br />
-
-
-
<br><b>Find Items that Contain:</b>
-
<input type=text name=targetstring size=20>
-
<br>
-
<br>
-
-
-
<table>
-
-
<tr>
-
<th>Item</th><th>Amount</th>
-
</tr>
-
-
-
<?php
-
-
-
$err_cnt = 0;
-
$total_amt = 0;
-
$read_ctr = 1;
-
-
-
-
for ($row = 1; $row <5 ; $row++) // creates rows that are less than 5
-
-
{
-
$item_name='item'.$row; // assign values
-
-
$array_counter = $row - 1; //To make sure to get element 0 in the array
-
-
-
-
$amount_name='amount'.$row; // assign values
-
-
$array_counter = $row - 1; //To make sure to get element 0 in the array
-
-
-
list($item_value_from_array, $amount_value_from_array) = explode('*',$sort_line[$array_counter]);
-
-
-
-
-
str_ireplace($targetstring, '<span style="background-color:#ff0000;">' . $targetstring . '</span>', $item_value); // can not figure out the string manipulation for the search text box
-
-
-
-
if ($targetstring == $item_value_from_array)
-
{
-
print "<td><input type=text name=$item_name value='".$item_value_from_array."' style=\"background-color: Yellow;\"></td>\n";
-
//print "<td><input type=text name=$item_name value='".$item_value_from_array."'></td>\n";
-
print "<td><input type=text name=".$amount_name." value='$amount_value_from_array'></td>\n";
-
} else {
-
print "<td><input type=text name=$item_name value='".$item_value_from_array."'></td>\n";
-
//print "<td><input type=text name=$item_name value='".$item_value_from_array."' style=\"background-color: Yellow;\"></td>\n";
-
print "<td><input type=text name=".$amount_name." value='$amount_value_from_array'></td>\n";
-
}
-
-
-
list($item_read, $amount_read) = explode("*", $sort_line[$array_counter]); // attempting to break up the Array elements into variables using explode function
-
-
-
-
if (!empty($amount_read)) //if amount value is not empty
-
{
-
if (is_numeric($amount_read)) // checks to see if amount value is a numeric number
-
{
-
$total = $total + $amount_read; //adds the total of the amount column and created the $total variable
-
} else {
-
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
-
$error_count++; // counts number of invalid amounts
-
}
-
}
-
-
print"</tr>\n";
-
}
-
-
-
?>
-
-
-
</table>
-
-
-
<?php
-
if ($error_count >0) // if statement checking to see if errors; if there are, then prints them. If not then prints total amount only
-
{
-
print"<br>Errors: $error_count"; // prints total of invalid amount errors
-
} else {
-
print"<br>Total: $total"; // prints total of amounts
-
}
-
-
-
?>
-
-
-
<br><br><input type=submit value=Submit>
-
-
<br>
-
-
<br>
-
-
-
</form>
-
</body>
-
</html>
1 1821 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: - if(stripos($string_to_search, $search_phrase) !== false) {
-
echo "They match!!";
-
}
-
else {
-
echo "Sorry, no match!";
-
}
Also, make sure you use the right value. That is: the $item_value_fro m_array variable, not the $item_value variable.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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) for my pupils.
This very simple thing does not seem simple at all.
If I use strings, I cannot replace their parts (though I can use...
|
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';
$sentence = "$insert or not $insert. That is the question.";
or
|
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
|
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 substrings need
be replaced, or one character needs be changed, what shall I do? Is it
better to convert strings to UCS-32 before manipulation?
But...
|
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 20.02.2006 etc...
And I want to replace the /,.etc character with - (as 20-02-2006)
| |
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
|
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 string”
This is very urgent. Please help
|
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 *strcpy(char *s1, const char *s2) // library function
|
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 number associated with that name.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |