Note that there are some explanatory texts on larger screens.

plurals
  1. POpreg_replace values in a string with values from mysql assoc while loop
    primarykey
    data
    text
    <p><strong>Edit:</strong> Updated the PHP to solve the array to string conversion issue in the $_POST handler, the while loop now displays the correct number of entries and replaces strings correctly, but only displays the replaced values for the first entry in the database for each loop through.</p> <p>I'm currently building a system which bulk messages people using a template system. Values contained in &lt;{}> tags will be replaced by values from a database result. For example, the message written may be:</p> <blockquote> <p>Hello &lt;{firstname}>&lt;{lastname}>, thank you for your purchase of &lt;{product}> on &lt;{date}>.</p> </blockquote> <p>With the replaced outcome as:</p> <blockquote> <p>Hello Joe Bloggs, thank you for your purchase of Smartphone on 01/01/2013.</p> </blockquote> <p>The fields to be replaced are dynamically created from the database, which in turn is created by the user uploading a CSV document. This means that the field names are not predictable, and I'm using the <code>show columns from table</code> SQL function to populate the names I will be looking for.</p> <p>I'm struggling with getting the values from my posted values to replaced the values in the string through the mysql_assoc loop.</p> <p>The HTML for the form inputs is created with a PHP loop from the <code>show columns</code> for each result, with each input looking like:</p> <pre><code>&lt;input type="hidden" name="fields[]" value="column name" /&gt; </code></pre> <p>And a textarea:</p> <pre><code>&lt;textarea name="message"&gt;&lt;/texarea&gt; </code></pre> <p>My PHP to handle this is:</p> <pre><code>if(isset($_POST['fields'])) { $strings = array(); foreach($fields as $f) { $strings[] = "/&lt;{".$f."}&gt;/"; } $replace = $_POST['fields']; } </code></pre> <p>And the current loop is:</p> <pre><code>$query = "select * from $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 as $r) { $replacement[] = $name[$r]; } $message = preg_replace($pattern, $replacement, $string); echo "&lt;p&gt;$message&lt;/p&gt;"; } </code></pre> <p>With <code>$table_name</code> being declared at the head of the document. </p> <p>The post works, and the arrays created appear to be correct, so I think my problem is with the contents of the while loop. As of right now, it returns nothing, how can I get the correct replacement values of <code>$name[]</code> into the <code>$replacement</code> array then loop through so that each message is formatted correctly? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload