I need to figure it out how to build a loop which number will depend on the number previously selected in another loop.
I'm building a small application to order some food. In my first loop in the HTML I added a snippet with php to select 1 to 5 element of a particular order:
<input type="checkbox" class="showHide" value="show">Spinach Dip Platter <span>$17.99 each</span>
<select name="spinach">
<option selected value> -- Select Quantity -- </option><?php
for ($i=0; $i<=5; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
</select>
Then, depends on how many items a customer is gonna select I want to give a limit to another loop, of a certain number. Let's say that he will select 2 items, in the next loop (which will be actually three loops of different breads) my customer can select only 2 items, instead of 5:
<label class="abread">
<input type="checkbox" class="showHidetwo" value="show" name="alligatorbread">With Alligator or Turtle Bread add $3.00<br>
<p>Turtle Bread</p>
<select name="turtle">
<option selected value> -- Select Quantity -- </option><?php
for ($i=0; $i<=5; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
</select>
<p>Aligator Bread</p>
<select name="alligator">
<option selected value> -- Select Quantity -- </option><?php
for ($i=0; $i<=5; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
</select>
<p>Spider Bread</p>
<select name="spider">
<option selected value> -- Select Quantity -- </option><?php
for ($i=0; $i<=5; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
</select>
</label>
Now the number is set to five, but I want to add a dynamic variable that will depend on the first loop (name="spinach") that will set the limit to the following loops. Is it possible?
UPDATE:
I tried something like that:
<label class="tray-items">
<input type="checkbox" class="showHide" value="show">Spinach Dip Platter <span>$17.99 each</span>
<p class="spinach">Sourdough</p>
<select name="spinach_sour">
<option selected value> -- Select Quantity -- </option><?php
for ($i=0; $i<=5; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
$spinach_sour = $_GET["spinach_sour"];
?>
</select>
<p class="spinach">Pumpernickle</p>
<select name="spinach-pumper">
<option selected value> -- Select Quantity -- </option><?php
for ($i=0; $i<=5; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
$spinach_pumper = $_GET["spinach_pumper"];
?>
</select>
<label class="abread">
<input type="checkbox" class="showHidetwo" value="show" name="alligatorbread">With Alligator or Turtle Bread add $3.00<br>
<p>Turtle Bread</p>
<select name="turtle">
<option selected value> -- Select Quantity -- </option><?php
for ($i=0; $i<=$spinach_pumper; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
</select>
<p>Aligator Bread</p>
<select name="alligator">
<option selected value> -- Select Quantity -- </option><?php
for ($i=0; $i<=5; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
</select>
<p>Spider Bread</p>
<select name="spider">
<option selected value> -- Select Quantity -- </option><?php
for ($i=0; $i<=5; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
</select>
</label>
</label>
Essentially I tried to get the first variable in a global get and after having assigned to the global varible I tried to export the value and reuse in the first of the second loop but it didn't work.
Matto, You can use variables in your for loop.
//make sure to declare your variable, but after that you can set the
value to whatever you like
$var = 0;
for ($i=0; $i<$var; $i++) {
//code goes here
}