Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the correct syntax with nested for() loops in php and has differentiating increments?
    primarykey
    data
    text
    <p>I am trying to build a loop structure where I know that maximum # of loops and where there is a separate increment increase for certain fields/inputs. With this in mind I am thinking that I should pursue a nested for() loop structure.</p> <p>As an example, let's say that the echoed goal, might look something like this:</p> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;input id="1" secondaryId="1" &gt;&lt;/td&gt; &lt;td&gt;&lt;input id="2" secondaryId="1" &gt;&lt;/td&gt; &lt;td&gt;&lt;input id="3" secondaryId="1" &gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input id="4" secondaryId="2" &gt;&lt;/td&gt; &lt;td&gt;&lt;input id="5" secondaryId="2" &gt;&lt;/td&gt; &lt;td&gt;&lt;input id="6" secondaryId="2" &gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input id="7" secondaryId="3" &gt;&lt;/td&gt; &lt;td&gt;&lt;input id="8" secondaryId="3" &gt;&lt;/td&gt; &lt;td&gt;&lt;input id="9" secondaryId="3" &gt;&lt;/td&gt; &lt;/tr&gt; &lt;!-- etc --&gt; &lt;/table&gt; </code></pre> <p>So the id in this example increase by one at every input but the secondaryId increases by after every three inputs. To help further visualize, consider the follow:</p> <p><img src="https://i.stack.imgur.com/BiJ1y.png" alt="example representation of the desired loop"></p> <p>I tried some things along this line for code:</p> <pre><code>&lt;?php echo"&lt;table&gt;"; for ($t=1;$t&lt;4;$t++){ echo"&lt;tr&gt;"; for($y=1;$y&lt;4;$y++){ echo"&lt;td&gt;&lt;input id='$y' secondaryId='$t'&gt;&lt;/td&gt;"; } echo"&lt;/tr&gt;"; } echo"&lt;/table&gt;"; ?&gt; </code></pre> <p>Quite obviously it is not working and echo's back this instead:</p> <p><img src="https://i.stack.imgur.com/3cB1Y.png" alt="example representation of what is happening with current efforts in loops"></p> <p>I see exactly why the problem is occurring, because at every iteration(right word?) of the parent loop, the <code>$y</code> variable is reset back to 1.</p> <p>How can I manipulate the loop(s) so that <code>$y</code> increments throughout the loop(s)? Is there a foreach combination that I ought to have considered?</p> <p>Also, let's say that the above "<code>id</code>" and "<code>secondaryId</code>" type attributes are fixed and cannot be changed.</p> <p>I have considered the last resort of just manually creating the field of inputs manually with the help of excel, but this seems laborious and promisses problems of debugging considering the field of inputs that I want to build is <em>50 rows</em> (<code>&lt;tr&gt;</code>) by <em>23 columns</em> (<code>&lt;td&gt;</code>)</p> <hr> <p><strong>> UPDATE:</strong></p> <p>I accepted Fabio's answer as it did work without fail, I wish I could have also accepted Maiden B.'s answer as this led me to create a mathematical solution in a single loop.</p> <p>The code I will end up using is:</p> <pre><code>$m = 0; echo"&lt;table&gt;"; for($y=1;$y&lt;4;$y++){ echo"&lt;tr&gt;&lt;td&gt; &lt;input id='".(1 + ($m*3))."' secondaryId='$y' &gt; &lt;input id='".(2 + ($m*3))."' secondaryId='$y' &gt; &lt;input id='".(3 + ($m*3))."' secondaryId='$y' &gt; &lt;/td&gt;&lt;/tr&gt;"; $m++; } echo"&lt;/table&gt;"; </code></pre> <hr>
    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