Can anyone help me figure out why the str_replace inside a while loop only replace first row in email template?
Following is the code:
$vehicle=mysql_query("select * from tbl_vehicle where book_id='$booking_Id'");
$i=0;
while($rows=mysql_fetch_array($vehicle)){
$make = $rows['make'];
$model = $rows['model'];
$message = str_replace("[make]",$make,$message);
$message = str_replace("[model]",$model,$message);
$i++;}
and the html template;
<tr>
<td>Car Make</td>
<td>[make]</td>
</tr>
<tr>
<td>Car Model</td>
<td>[model]</td>
P.S I'm fetching template from Database.
Issue is when one vehicle booked it works fine but if more then one vehicle is booked it only replace first vehicle detail in email.
Thanks for the help.
EDITED;
e.g if 1 vehicle booked, in email template it shows
Make: Make-One
Model: Model-One
If 2 vehicles booked, in email template is shows
Make: Make-One
Model: Model-One
but it should show
Make: Make-One
Model: Model-One
Make: Make-Two
Model: Model-Two
Hope this clears the question.
UPDATE; Simon_w solution didn't work, but gives me an idea (i don't know its best approach or not)
while($rows=mysql_fetch_array($vehicle)){
$make = $rows['make'];
$model = $rows['model'];
$i++;
if($i==2) {
$message = str_replace("[make]",$make,$message);
$message = str_replace("[model]",$model,$message);
$message = str_replace("[make2]",$make,$message);
$message = str_replace("[model2]",$model,$message);
}
else {
$message = str_replace("[make]",$make,$message);
$message = str_replace("[model]",$model,$message);
}
}
and in email template
<tr>
<td>Car Make</td>
<td>[make]</td>
</tr>
<tr>
<td>Car Model</td>
<td>[model]</td>
<tr>
<td>Car Make</td>
<td>[make2]</td>
</tr>
<tr>
<td>Car Model</td>
<td>[model2]</td>
So this solves the 2 vehicle issue, both detail showing but if only 1 vehicle booked then in email template [make2] [model2] shows blank so is there any way to use if statement in email template too?
str_replaceright? github.com/r3wt/superuser/blob/master/libs/custom/…str_replace()will replace all occurrences in a string (i.e. all your placeholders) meaning when you hit the second DB record, there are no placeholders left to replace. You could usepreg_replace()with a limit of 1 instead:preg_replace('/\[make\]/', $make, $message, 1)