I'm trying to loop 2 variables 开发者_StackOverflow社区and with an output that looks like this '91 - 96 lbs' I can get the For statement to work with just one variable but with two it does not work.
for ($k = 91; $k <= 496; $k=$k+4($i = 96; $i <= 500; $i=$i+4))
echo '<option value='.$k. ' - ' .$i. ' lbs'("<%m_weight%>" == .$k. ' - ' .$i. ' lbs' ? ' selected="selected"' : '').'>'.$k. ' - ' .$i. ' lbs</option>';
You probably don't actually want two variables there:
for ($k = 91; $k <= 496; $k=$k+5)
echo '<option value='.$k. ' - ' .($k+4). ' lbs'("<%m_weight%>" == .$k. ' - ' .($k+4). ' lbs' ? ' selected="selected"' : '').'>'.$k. ' - ' .($k+4). ' lbs</option>';
you don't need two variables, one would be enough
if you really, really, really want two variables, just use coma to separate statements and logical operators to combine tests.
for ($k = 91, $i = 96 ; ($k <= 496) || ($i <= 500) ; $k=$k+4, $i= $i + 4) { echo "$k - $i lbs"; }
As you can see the end test now looks quite silly (both parts of the test become true at the same time)... that's one more hint that you didn't really wanted two variables at all.
You will need to nest your for loops. That syntax is incorrect
for ($k = 91; $k <= 496; $k=$k+4)
{
for($i = 96; $i <= 500; $i=$i+4)
{
//more code here.
}
}
You need to nest them correctly:
for ($k = 91; $k <= 496; $k=$k+4) {
for ($i = 96; $i <= 500; $i=$i+4) {
echo '<option value='.$k. ' - ' .$i. ' lbs'("<%m_weight%>" == .$k. ' - ' .$i. ' lbs' ? ' selected="selected"' : '').'>'.$k. ' - ' .$i. ' lbs</option>';
}
}
No need for two for loops, or two variables at all:
for ($k = 91; $k <= 496; $k=$k+5)
echo '<option value='.$k. ' - ' .($k+4). ' lbs'.($m_weight == $k ? ' selected="selected"' : '').'>'.$k. ' - ' .($k+4). ' lbs</option>';
Check what format $m_weight is in; your syntax there was garbled.
You can put multiple operations in the initialization and increment portion of a for loop using a ,
for ($k=91, $i=96; $k<=496 && $i<=500; $k+=4, $i+=1) {
}
精彩评论