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
String manipulation question - Text Box Searching
Old 05-03-2010, 01:49 AM String manipulation question - Text Box Searching
Junior Talker

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

PHP Code:
<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>&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 
    
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
    
} 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 <$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>
pazazuzu is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-04-2010, 04:03 AM Re: String manipulation question - Text Box Searching
Average Talker

Posts: 22
Trades: 0
You may check function strpos with "&&" condition , along with preg_match function, because you may need to match a char in addition to a string.
__________________

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
katierosy is offline
Reply With Quote
View Public Profile Visit katierosy's homepage!
 
Old 05-06-2010, 10:16 PM Re: String manipulation question - Text Box Searching
Skilled Talker

Posts: 69
Name: Greg
Location: South Carolina
Trades: 0
unless I am misunderstanding you that can not be done. Reason being is that you are trying to tie the php code to the front end when it sits on the server.

so for this to work the page would have to be refreshed.

this can be done with javascript though.

you are trying to do this correct?

have a form, with a field. as the user types, if the field contains xyz, then highlight the field?

you would need to post the form, refreshing the page, and checking the field with php and conditionally setting the style.
scuts is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to String manipulation question - Text Box Searching
 

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.54535 seconds with 12 queries