Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How to search a string? String Manipulation
Old 04-14-2010, 11:26 AM How to search a string? String Manipulation
Junior Talker

Posts: 3
Name: Miguel
Trades: 0
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

PHP Code:
 
<html>
<head>
<title>My Bills</title>
</head>
 
<body>
 
<body style="font-family: Arial, Helvetica, sans-serif; color:
black;">
 
<h1>My Bills</h1>
 
 
<form method=post>
 
<br><b>Sort order for items:</b>
<br>
<br>
 
<?php
 
$sort_order 
$_POST['sort_order']; //added code for asgn 6
 
$targetstring $_POST['targetstring']; //added code for asgn 7
 
 
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>&nbsp;&nbsp;&nbsp;"
;
print 
"Descending <input type=radio name=sort_order value=desc
checked>&nbsp;&nbsp;&nbsp;"
;
break;
 
default :
 
print 
"Ascending <input type=radio name=sort_order value=asc
checked>&nbsp;&nbsp;&nbsp;"
;
print 
"Descending <input type=radio name=sort_order
value=desc>&nbsp;&nbsp;&nbsp;"
;
break;
 
}
 
 
$sort_line = array(); //Declare an empty array - added code for asgn
6
 
 
for ($row 1$row <$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 - added code for asgn 6
} else {
sort($sort_line); //Sorts array in place - added code for asgn 6
}
 
 
?>
 
 
<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 <$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 - added code for asgn 6
 
 
$amount_name
='amount'.$row// assign values
 
$array_counter $row 1//To make sure to get element 0 in the
array - added code for asgn 6
 
 
list($item_value_from_array$amount_value_from_array) = explode('*',
$sort_line[$array_counter]);
 
 
print
"<tr>\n";
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
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
 
 
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 arethen 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>

Last edited by mgraphic; 04-14-2010 at 02:57 PM.. Reason: reset bbcode formating
pazazuzu is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-14-2010, 11:45 AM Re: How to search a string? String Manipulation
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Something like this:
PHP Code:
str_replace($target_string'<span style="background-color:#ff0000;">' $target_string '</span>'$item_value); 
Use str_ireplace instead for a case-insensitive search.

Also, when using php bbcode you're meant to not include the space and red color formatting.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-14-2010, 11:59 AM Re: How to search a string? String Manipulation
Junior Talker

Posts: 3
Name: Miguel
Trades: 0
Thank you. Sorry about the improper code posting.

I will give your advice a try. I guess the answer is simple but I am not sure why I have been having such trouble finding a good example for this solution. Maybe it's just over my head.

thank you for your help.
pazazuzu is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How to search a string? String Manipulation
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25059 seconds with 12 queries