jbwebdesign Posted November 14, 2010 Report Share Posted November 14, 2010 Hello everyone, I'm working on a script and i'm having a problem getting it to do what i want. Basically i am looking to pull content from a MySQL database, search the string of content for a specific word(text) and replace it with another word(text) from a different table in the same database. I have created a loop to loop through the table with words that are going to replace the words of this string. The only problem is that it loops the content 3 times. the first time, it loops through the content and replaces the 1st word in the table, the 2nd time it loops through the content and replaces the 2nd word in the table but the first word isn't replaced anymore etc. can someone please help? here is my code: <h2>Generated Contract</h2> <div id="success"><?php echo $status; ?></div> <?php $contract_id = $_POST['contractID']; $client_id = $_POST['clientID']; $sql = "SELECT * FROM " . TABLE_CONTRACTS . " WHERE id=" . $contract_id; // feed it the sql directly. store all returned rows in an array $rows = $db->fetch_all_array($sql); // get customized variables to tie into the contract $sql2 = "SELECT variable,value FROM " . TABLE_VARIABLES; $rows2 = $db->query($sql2); // print out array later on when we need the info on the page foreach($rows as $record){ ?> <div id="Contract"> <form action="?sm=edit_contract&update=yes" method="post"> <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" /> <label for="contract_type">Contract Type:</label><input type="text" id="contract_type" name="contract_type" value="<?php echo $record['contract_type']; ?>"> <label for="contract_name">Contract Name:</label><input type="text" id="contract_name" name="contract_name" value="<?php echo $record['contract_name']; ?>"> <br /><br /> <label for="contract_content">Contract Content:</label> </div> <div id="ContractContent"> <textarea class="ckeditor" cols="80" id="contractContent" name="contract_content" rows="10"> <?php while($key = $db->fetch_array($rows2)){ if(stristr($record['contract_content'],$key['variable'])){ echo str_replace($key['variable'],$key['value'],$record['contract_content']); } } echo $record['contract_content']; ?> </textarea> <input type="submit" value="Print Contract!"> </form> </div> <?php }//end for loop ?> Quote Link to comment Share on other sites More sharing options...
falkencreative Posted November 14, 2010 Report Share Posted November 14, 2010 Perhaps you can explain a bit better exactly what the issue is? The way I see it, you have two loops -- a foreach loop that loops for every record in the first MySQL query $rows. Within that foreach loop, there is a while loop that loops for every record in the second query $rows2. You should (ideally?) get something like this: -- loops through $row record 1 -- loops through $row2 record 1 -- loops through $row2 record 2 -- loops through $row2 record 3 -- loops through $row2 record 4 -- loops through $row record 2 -- loops through $row2 record 1 -- loops through $row2 record 2 -- loops through $row2 record 3 -- loops through $row2 record 4 -- loops through $row record 3 -- loops through $row2 record 1 -- loops through $row2 record 2 -- loops through $row2 record 3 -- loops through $row2 record 4 -- loops through $row record 4 -- loops through $row2 record 1 -- loops through $row2 record 2 -- loops through $row2 record 3 -- loops through $row2 record 4 Quote Link to comment Share on other sites More sharing options...
jbwebdesign Posted November 14, 2010 Author Report Share Posted November 14, 2010 (edited) i'm sorry, maybe this is a better explanation...... i have a database with 2 different tables. table 1 is called variables table 2 is called contracts the variables table has 3 columns, id variable value this is kind of difficult for me to explain but here is an example: lets say the variable table has a variable called "{#Apple}" and the value of that variable is "Red" then i would like for PHP to Search the string $record['contract_content'] for the variable "{#Apple}" and to replace that variable with the value of it which is "Red". however, there is multiple variables, there for i want it to loop through all of the variables and replace all of them with the value. Edited November 14, 2010 by jbwebdesign Quote Link to comment Share on other sites More sharing options...
falkencreative Posted November 14, 2010 Report Share Posted November 14, 2010 Hate to say it, but I'm not sure if I understand what you are trying to do. Have you tried using print_r() to print out $row and $row2 to ensure they hold the records you expect them to? Quote Link to comment Share on other sites More sharing options...
jbwebdesign Posted November 14, 2010 Author Report Share Posted November 14, 2010 yes they hold the records, let me try to explain a bit differently........ i have a table structure as follows: (id, variable, value) 1, {#Apple}, Red 2, {#Banana}, Yellow 3, {#Orange}, Orange now i have a string: $string = "blah blah blah blah blah blah blah {#Apple} blah blah blah blah blah blah {#Banana} blah blah blah blah blah {#Orange}"; As you can see above, i have a table with 3 different rows. I want to replace the variables in my string with the actual value from the table. so my new string would be: $new_string = "blah blah blah blah blah blah blah Red blah blah blah blah blah blah Yellow blah blah blah blah blah Orange"; Quote Link to comment Share on other sites More sharing options...
falkencreative Posted November 14, 2010 Report Share Posted November 14, 2010 I understand your explanation above. How is your current code not working? You said "The only problem is that it loops the content 3 times." It's looping too many times? Too few times? Quote Link to comment Share on other sites More sharing options...
jbwebdesign Posted November 14, 2010 Author Report Share Posted November 14, 2010 it's looping the string 3 times 1st time it gets the value of the first variable {#Apple} and replaces it with "Red" 2nd time it gets the value of the second variable {#Banana} and replaces it with "Yellow", however the 1st variable is {#Apple} instead of "Red". 3rd time it gets the value of the third variable {#Orange} and replaces it with "Orange" , but the 1st and 2nd variables are back to {#Apple} and {#Banana} ---------------------------------- I do not want to loop the string. I want to loop through the table with all the different variables and if the string contains any of the variables in the table then i want to replace them with the value. but i only want to have 1 string not 3 Quote Link to comment Share on other sites More sharing options...
falkencreative Posted November 14, 2010 Report Share Posted November 14, 2010 I think I've figured out your problem. Take a look at this sample code: <?php $temp[0]['0'] = "{#Apple}"; $temp[0]['1'] = "Red"; $temp[1]['0'] = "{#Banana}"; $temp[1]['1'] = "Yellow"; $temp[2]['0'] = "{#Orange}"; $temp[2]['1'] = "Orange"; $string = "Test string: {#Apple} {#Banana} {#Orange}"; foreach($temp as $row) { if(stristr($string,$row[0])) { //echo str_replace($row[0],$row[1],$string); // will result $string not changing $string = str_replace($row[0],$row[1],$string); // will result in string changing } } echo $string; ?> If you test this, you'll notice that the "echo" line will result in $string not changing. You'll get "Test string: {#Apple} {#Banana} {#Orange}" as the result. However, if you set $string = to the str_replace(), that gives you the results you need. Perhaps this will help you fix your code? I think if you replace this line echo str_replace($key['variable'],$key['value'],$record['contract_content']); with $record['contract_content'] = str_replace($key['variable'],$key['value'],$record['contract_content']); You'll get the results you want. If not, hopefully it will give you some hints as to your issue. Obviously nothing in your code currently saves the changes back to the database, but I'm assuming you'll work on that later once you are getting correct results. Quote Link to comment Share on other sites More sharing options...
jbwebdesign Posted November 14, 2010 Author Report Share Posted November 14, 2010 it works! thanks, and yes i am going to do the database work now that i have this correct Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.