Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - Nested Looping Trouble
    text
    copied!<p>I have an HTML table that I need to populate with the values grabbed from a select statement. The table cols are populated by an array (0,1,2,3). Each of the results from the query will contain a row 'GATE' with a value of (0-3), but there will not be any predictability to those results. One query could pull 4 rows with 'GATE' values of 0,1,2,3, the next query could pull two rows with values of 1 &amp; 2, or 1 &amp; 3.</p> <p>I need to be able to populate this HTML table with values that correspond. So HTML COL 0 would have the TTL_NET_SALES of the db row which also has the GATE value of 0.</p> <pre><code>&lt;?php $gate = array(0,1,2,3); $gate_n = count($gate); /* Database = my_table.ID my_table.TT_NET_SALES my_table.GATE my_table.LOCKED */ $locked = "SELECT * FROM my_table WHERE locked = true"; $locked_n = count($locked); /* EXAMPLE RETURN Row 1: my_table['ID'] = 1 my_table['TTL_NET_SALES'] = 1000 my_table['GATE'] = 1; Row 2: my_table['ID'] = 2 my_table['TTL_NET_SALES'] = 1500 my_table['GATE'] = 3; */ print "&lt;table border='1'&gt;"; print "&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;"; print "&lt;tr&gt;"; for ($i=0; $i&lt;$locked_n; $i++) { for ($g=0; $g&lt;$gate_n; $g++) { if (!is_null($locked['TTL_NET_SALES'][$i]) &amp;&amp; $locked['GATE'][$i] == $gate[$g]) { print "&lt;td&gt;$".$locked['TTL_NET_SALES'][$i]."&lt;/td&gt;"; } else { print "&lt;td&gt;-&lt;/td&gt;"; } } } print "&lt;/tr&gt;"; print "&lt;/table&gt;"; /* What I want to see: &lt;table border='1'&gt; &lt;tr&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-&lt;/td&gt; &lt;td&gt;1000&lt;/td&gt; &lt;td&gt;-&lt;/td&gt; &lt;td&gt;1500&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; */ ?&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