Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming your input is a string with line-breaks... First process your data into an keyed array to make life easier... I've used regexes to make it more robust against formatting changes.</p> <pre><code>$eventdays="Monday = Mo Tuesday = Tu Wedneday = We Thursday = Th Friday = Fr Saturday = Sa Sunday = Su"; $lines = explode("\n", $eventdays); $data = array(); foreach ($lines as $line) { if (preg_match("/(\w+)\s*=\s*(\w+)/", $line, $match)) { $data[] = array('value'=&gt;$match[2], 'label' =&gt; $match[1]); } } </code></pre> <p>Now just iterate over the structure printing out the keys / labels. Use the in_array function to check if the current one should be selected. Also I used checked="checked" which is the standards-compliant way of selecting checkboxes... See <a href="https://stackoverflow.com/questions/2874949/what-is-the-syntax-for-boolean-attributes-e-g-a-checked-checkbox-in-html">this question</a>.</p> <pre><code>$selected_test="Mo,We,Sa"; $select=explode(",", $selected_test); foreach ($data as $datum) { $checked=""; if (in_array($datum['value'], $select)) { $checked = " checked=\"checked\""; } echo &lt;&lt;&lt; EOF &lt;input type="checkbox" name="days[]" value="{$datum['value']}"$checked/&gt;{$datum['label']}&lt;br /&gt;\n EOF; } </code></pre> <p>Output</p> <pre><code>&lt;input type="checkbox" name="days[]" value="Mo" checked="checked"/&gt;Monday&lt;br /&gt; &lt;input type="checkbox" name="days[]" value="Tu"/&gt;Tuesday&lt;br /&gt; &lt;input type="checkbox" name="days[]" value="We" checked="checked"/&gt;Wedneday&lt;br /&gt; &lt;input type="checkbox" name="days[]" value="Th"/&gt;Thursday&lt;br /&gt; &lt;input type="checkbox" name="days[]" value="Fr"/&gt;Friday&lt;br /&gt; &lt;input type="checkbox" name="days[]" value="Sa" checked="checked"/&gt;Saturday&lt;br /&gt; &lt;input type="checkbox" name="days[]" value="Su"/&gt;Sunday&lt;br /&gt; </code></pre>
 

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