Maracles Posted November 7, 2009 Share Posted November 7, 2009 I am trying to use the below loop to echo the following code but I am having two problems; 1. Each time the loop runs I need the variable $day1 to increase to $day2. Is it possible to use the $i variable here somehow? 2. The line here is incorrect, I think because I am unsure how to create a nested loop like is required here. '.if ($day1 > 0) {echo $day1;}else {echo '0';}.' Can anyone help me get this working? I'm very grateful for all help. <?php for ($i=1; $i < 29; $i++) { echo '<label>Day <b>'.$i.'</b> - $</label>'; echo '<input class="input" type="text" name="day'.$i.'" maxlength="10" size="10" value="'.if ($day1 > 0) {echo $day1;}else {echo '0';}.' " disabled="disabled"/> $'; echo '<input class="input" type="text" name="day'.$i.'new" maxlength="10" size="10" /><br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/180615-solved-increasing-a-number-in-a-variable-name/ Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2009 Share Posted November 7, 2009 here you go <?php for ($i=1; $i < 29; $i++) { echo '<label>Day <b>'.$i.'</b> - $</label>'; echo '<input class="input" type="text" name="day'.$i.'" maxlength="10" size="10" value="'.((${"day$i"} > 0) ? ${"day$i"} : '0').' " disabled="disabled"/> $'; echo '<input class="input" type="text" name="day'.$i.'new" maxlength="10" size="10" /><br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/180615-solved-increasing-a-number-in-a-variable-name/#findComment-952906 Share on other sites More sharing options...
Maracles Posted November 7, 2009 Author Share Posted November 7, 2009 Thank you again, that has worked perfectly. One more questions to help me understand if you don't mind: What is the '?' and the ':' for? Is is short hand for 'then' and 'equals'? Link to comment https://forums.phpfreaks.com/topic/180615-solved-increasing-a-number-in-a-variable-name/#findComment-952913 Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2009 Share Posted November 7, 2009 its the shorthand for "if control structure" the Ternary Operator http://php.net/manual/en/language.operators.comparison.php scroll down a bit or search for Ternary operator Link to comment https://forums.phpfreaks.com/topic/180615-solved-increasing-a-number-in-a-variable-name/#findComment-952916 Share on other sites More sharing options...
Maracles Posted November 7, 2009 Author Share Posted November 7, 2009 Will look at that later. Thank you. Link to comment https://forums.phpfreaks.com/topic/180615-solved-increasing-a-number-in-a-variable-name/#findComment-952917 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.