Monday, 15 August 2011

php - preg_replace values in a string with values from mysql assoc while loop -



php - preg_replace values in a string with values from mysql assoc while loop -

edit: updated php solve array string conversion issue in $_post handler, while loop displays right number of entries , replaces strings correctly, displays replaced values first entry in database each loop through.

i'm building scheme mass messages people using template system. values contained in <{}> tags replaced values database result. example, message written may be:

hello <{firstname}><{lastname}>, give thanks purchase of <{product}> on <{date}>.

with replaced outcome as:

hello joe bloggs, give thanks purchase of smartphone on 01/01/2013.

the fields replaced dynamically created database, in turn created user uploading csv document. means field names not predictable, , i'm using show columns table sql function populate names looking for.

i'm struggling getting values posted values replaced values in string through mysql_assoc loop.

the html form inputs created php loop show columns each result, each input looking like:

<input type="hidden" name="fields[]" value="column name" />

and textarea:

<textarea name="message"></texarea>

my php handle is:

if(isset($_post['fields'])) { $strings = array(); foreach($fields $f) { $strings[] = "/<{".$f."}>/"; } $replace = $_post['fields']; }

and current loop is:

$query = "select * $table_name"; $result = mysql_query($query) or die(mysql_error()); $string = $_post['message']; $pattern = $strings; while($name = mysql_fetch_array($result, mysql_assoc)) { foreach ($replace $r) { $replacement[] = $name[$r]; } $message = preg_replace($pattern, $replacement, $string); echo "<p>$message</p>"; }

with $table_name beingness declared @ head of document.

the post works, , arrays created appear correct, think problem contents of while loop. of right now, returns nothing, how can right replacement values of $name[] $replacement array loop through each message formatted correctly?

you should take @ sprintf, might easier.

php arrays while-loop preg-replace

No comments:

Post a Comment