xander85 Posted September 19, 2011 Share Posted September 19, 2011 Hi All, I'm jumping back into PHP and I have a basic loop question that I'm struggling with. I'm using a "date picker" code to select times during the day and then using a javascript to calculate the total hours for the day. However, I'm struggling with the loop to print out the fields. I need a loop that will print the following: first iteration: value = 1 value = 2 value = 0 second iteration: value = 2 value = 3 value = 1 The code I'm using is the following: for ($id = 1; $id <=45 ; $id++) { ?> <?php if($id % 2) { ?> <div id="labor_template_day_100"> <input type="text" name="time1000<?php echo $id; ?>""/> <input type="text" name="time1000<?php echo $id+1; ?>" /> <?php } ?> <input type="hidden" id="emp<?php echo $id-1; ?>hours" value="0"/> <?php if($id % 2) { ?> </div> <?php } } This prints: first iteration: value = 1 value = 2 value = 0 Inserts just a hidden form field, then: value = 1 value = 2 value = 2 See below: <div id="labor_template_day_100"> <input type="text" name="time10001" id="time10001" size="7" value="06:00 AM" onChange="CalcHrs('time10001', 'time10002', '0');"/> <input type="text" name="time10002" id="time10002" size="7" value="06:00 AM" onChange="CalcHrs('time10001', 'time10002', '0');" /> <input type="hidden" id="emp0hours" value="0"/> </div> <input type="hidden" id="emp1hours" value="0"/> <div id="labor_template_day_100"> <input type="text" name="time10003" id="time10003" size="7" value="06:00 AM" onChange="CalcHrs('time10003', 'time10004', '2');"/> <input type="text" name="time10004" id="time10004" size="7" value="06:00 AM" onChange="CalcHrs('time10003', 'time10004', '2');" /> <input type="hidden" id="emp2hours" value="0"/> </div> <input type="hidden" id="emp3hours" value="0"/> I have a feeling this is something basic I need to change. Link to comment https://forums.phpfreaks.com/topic/247402-loop-question/ Share on other sites More sharing options...
xander85 Posted September 19, 2011 Author Share Posted September 19, 2011 Sorry. I need the sequence to be the following: First iteration: value = 1 value = 2 value = 1 second iteration: value = 3 value = 4 value = 2 third iteration: value = 5 value = 6 value = 3 Thanks! Link to comment https://forums.phpfreaks.com/topic/247402-loop-question/#findComment-1270527 Share on other sites More sharing options...
xyph Posted September 19, 2011 Share Posted September 19, 2011 <?php for( $i = 1; $i <= 5; $i += 2 ) { echo $i.','.($i+1).','.(($i+1)/2).'<br>'; } ?> Link to comment https://forums.phpfreaks.com/topic/247402-loop-question/#findComment-1270541 Share on other sites More sharing options...
xander85 Posted September 19, 2011 Author Share Posted September 19, 2011 Works great, thanks! Link to comment https://forums.phpfreaks.com/topic/247402-loop-question/#findComment-1270665 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.