Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You double-bubble the loopy-dupy.</p> <p>Just take the foreach, remove the for inside. Drop <code>$i</code> and replace <code>$units</code> with <code>$unit</code>. See as well <a href="http://php.net/foreach" rel="nofollow">http://php.net/foreach</a>.</p> <p>Example:</p> <pre><code>$units = json_decode($tablerow); foreach ($units as $unit) : ?&gt; &lt;tr class="rows"&gt; &lt;td&gt;&lt;input type='text' name='bedroom[]' value='&lt;?=$unit[0];?&gt;' class='input-small'&gt;&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='bath[]' value='&lt;?=$unit[1];?&gt;' class='input-small'&gt;&lt;/td&gt; ... </code></pre> <p>There is a fixed (1:1) relationship between the sub-value (e.g. bedroom, bath) and the index in the <code>$unit</code>. You can hard-encode it (bedroom = 0, bath = 1, ...).</p> <p>You could also provide a transposing from numeric indexes into named properties by wrapping into a decorating iterator creating each subtype on the the fly. This can also help you to turn <code>yes</code> and <code>no</code> into <code>true</code> and <code>false</code> for example. But if this is only one-time output, I'd say it's not yet worth.</p> <p>An intermediate is to move initialization code to the top of the loop:</p> <pre><code>foreach ($units as $unit) { $unit[4] = $unit[4] === 'yes'; $unit= array_map('intval', $unit); # escape all values to prevent injection ?&gt; ... &lt;td&gt; &lt;select name='avail[]' class='input-small'&gt; &lt;option value='yes' &lt;?= $unit[4] ? 'selected="selected' : '';?&gt;&gt;Yes&lt;/option&gt; &lt;option value='no' &lt;?= !$unit[4] ? 'selected="selected' : '';?&gt;&gt;No&lt;/option&gt; &lt;/select&gt; &lt;/td&gt; ... &lt;? } ?&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