I am using two columns in a table and am using this code to try to loop the results however this only makes changes on the last row. I have tried reorganizing the loop but don't seem to be getting it right. Is there another "fetch" option or should I change the coding itself? I want the str_replace to include all rows in the database and make replacements for each one not just one at a time so it needs to loop through the database and match all rows that it finds a match for. I thought I could just match the $newstring but it doesn't seem to be working... I tried putting all of the equation inside the loop but then it gives me three different outputs with still only 1 change each output. I need all the changes combined into one output. $string = $_POST['data']; while($row = mysql_fetch_array( $result )) { $newstring = str_replace($row[one],$row[two],$string); }
Here is what I am using now.. but I am still getting only the last row.. I need all the rows. $result=mysql_query("select * from TABLE"); while($row=mysql_fetch_array($result)){ $matchArray = $row[COLUMN]; $replaceArray = $row[COLUMN]; } $newstring = str_replace($matchArray,$replaceArray,$string); echo $_POST['data']; echo $newstring; This is from a post.. It posts to the script.. the script matches text from the post and then gives me both the old text and the changed text. I am trying to use instead of a hard coded array()... a databases for the array values where it will match and replace all values from the database if they exist.
I've tried it but the results I'm getting are not exactly what I want. If I put the echo() in the loop then I get this: 1:This is NEW one This is two this is three 2:This is one This is NEW two this is three 3:This is one This is two this is NEW three But what I want to get is: 1:This is NEW one This is NEW two This is NEW three.
try this: $string = $_POST['data']; while($row = mysql_fetch_array( $result )) { $string = str_replace($row[one],$row[two],$string); } PHP: you assigned the modified string to a new variable, named $newstring. So when looping over the resultset, you change the current $row[one], and assigns the changed string to a new variable ($newstring). What you had to do was update the current variable ($string).
UnrealEd.. rep added.. thank you SOOOOO much. The logic... it's almost unthinkable.. (A NOT = B) BUT (A+B) = A...