deib97 Posted January 6, 2014 Share Posted January 6, 2014 (edited) I want to generate looping with php. Can anyone tell me what I am doing for make it? I was upload excel on dropbox, maybe someone in this forum can help me? https://www.dropbox.com/s/e57spr9qi7o5749/simulasi.xls thanks Edited January 6, 2014 by deib97 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 6, 2014 Share Posted January 6, 2014 (edited) The sequence is simple. It is adding 1 to the previous number and then subtracting 7 if the new number is greater than 7, (resting the number sequence to 1 again) <?php // create emtpy array with 31 items set to zero $seq = array_fill(0, 31, 0); // set 1st, 15th and 26th postions with predifined number $seq[0] = 5; // 1st $seq[14] = 1; // 15th $seq[25] = 6; // 26th // generate sequance for($i = 1; $i < 31; $i++) { // if current postion in sequance is greater than 0, skip it // these are our predefined numbers in the sequance if($seq[$i] > 0) continue; // work out next number in sequance // gets the previous number and adds 1 $num = $seq[$i - 1] + 1; // if number is greater than 7, take 7 away if($num > 7) $num -= 7; // set the number $seq[$i] = $num; } ?> <style> th { color: red; width: 20px;} td { text-align: center; } </style> <table> <tr> <th><?php echo implode('</th><th>', range(1, 31)); ?></th> </tr> <tr> <td><?php echo implode('</td><td>', $seq); ?></td> </tr> </table> Edited January 6, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
deib97 Posted January 7, 2014 Author Share Posted January 7, 2014 Many thanks Ch0cu3r... Quote Link to comment Share on other sites More sharing options...
deib97 Posted January 7, 2014 Author Share Posted January 7, 2014 Expansion case for schedule shift. https://dl.dropboxusercontent.com/s/2ksm9i7mrk91dd0/simulasi%20Roster.xls?dl=1&token_hash=AAGI7XXQjXJXSWQs7NQMbUTCBgOoCgn9xOKZnaYRbFYdsA On this excel Hari ke u/ jadwal kerja is same solution from "Ch0cu3r", done This shift is determined by the pattern. example : 7/1 : 1 until 6x, 1 off ; if patern 14/1 : 1 until 6x, 2 until 7x and 1 Off. Please help me bro.. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 7, 2014 Share Posted January 7, 2014 <?php // array for lembur sequane $seq['lembur'] = array(); $seq['lembur'][0] = 3; // array for kerja sequance $seq['kerja'] = array(); $seq['kerja'][0] = 1; // array for shift sequance $seq['shift'] = array(); // the max number foreach sequance $seqances = array('lembur' => 7, 'kerja' => 14); // generate the sequances for($i = 0; $i < 31; $i++) { // generate the number sequances for lembur and kerja foreach($seqances as $key => $maxSeq) { // if current postion is set, then skip it if(isset($seq[$key][$i])) continue; // get previous number in current squance $num = $seq[$key][$i - 1] + 1; // is number greater than the max sequance number // reset sequance if($num > $maxSeq) $num -= $maxSeq; // add number to current sequance $seq[$key][$i] = $num; } // the shift sequance if($seq['kerja'][$i] < 7) $shift = 1; elseif($seq['kerja'][$i] < 14) $shift = 2; else $shift = 'Off'; $seq['shift'][$i] = $shift; } ?> <style> th { color: red; width: 20px;} td { text-align: center; } </style> <table> <tr> <th>Tanggal</th> <th><?php echo implode('</th><th>', range(1, 31)); ?></th> </tr> <?php foreach($seq as $key => $sequance): ?> <tr> <td><?php echo ucwords($key) ?></td> <td><?php echo implode('</td><td>', $sequance); ?></td> </tr> <?php endforeach; ?> </table> Quote Link to comment Share on other sites More sharing options...
deib97 Posted January 8, 2014 Author Share Posted January 8, 2014 thank you for your support and understanding in this case "Cho0cu3r" Awesome... :happy-04: Quote Link to comment Share on other sites More sharing options...
deib97 Posted January 8, 2014 Author Share Posted January 8, 2014 (edited) Hi, Cho0cu3r I have combination again. Im happy and try your code, but when I try to merge with my combination, the result not same with my simulation. How to change shift if pattern switch to 7/1 : 6x day 1 off with dinamic shift 1 or 2. This my code : <?php // array for lembur sequane $seq['lembur'] = array(); $seq['lembur'][0] = 5; // array for kerja sequance $seq['kerja'] = array(); $seq['kerja'][0] = 6; $seq['kerja2'] = array(); $seq['kerja2'][0] = 5; // array for shift sequance $seq['shift'] = array(); $seq['shift2'] = array(); // the max number foreach sequance $seqances = array('lembur' => 7, 'kerja' => 14, 'kerja2' => 7); // generate the sequances for($i = 0; $i < 31; $i++) { // generate the number sequances for lembur and kerja foreach($seqances as $key => $maxSeq) { // if current postion is set, then skip it if(isset($seq[$key][$i])) continue; // get previous number in current squance $num = $seq[$key][$i - 1] + 1; // is number greater than the max sequance number // reset sequance if($num > $maxSeq) $num -= $maxSeq; // add number to current sequance $seq[$key][$i] = $num; } // the shift sequance if($seq['kerja'][$i] < 7) $shift = 1; elseif($seq['kerja'][$i] < 14) $shift = 2; else $shift = 'Off'; if($seq['kerja2'][$i] < 7) $shift2 = 2; // change automatically 1 or 2 after off else $shift2 = 'Off'; $seq['shift'][$i] = $shift; $seq['shift2'][$i] = $shift2; } ?> <style> th { color: red; width: 20px;} td { text-align: center; } </style> <table> <tr> <th>Tanggal</th> <th><?php echo implode('</th><th>', range(1, 31)); ?></th> </tr> <?php foreach($seq as $key => $sequance): ?> <tr> <td><?php echo ucwords($key) ?></td> <td><?php echo implode('</td><td>', $sequance); ?></td> </tr> <?php endforeach; ?> </table> Based on your experience, Wheter such transactions should be stored in a database, with the target job to calculate the salary and overtime? Edited January 8, 2014 by deib97 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 8, 2014 Share Posted January 8, 2014 Add $x = 0; before the for loop Now change the shift2 code to if($seq['shift2'][$i] < 7) $shift2 = ($x % 2 == 0) ? 2 : 1; // alternate between 2 and 1 elseif($seq['shift2'][$i] == 7) { $shift2 = 'Off'; $x++; // increment x for each 'Off' } Quote Link to comment Share on other sites More sharing options...
deib97 Posted January 8, 2014 Author Share Posted January 8, 2014 Add $x = 0; before the for loop Now change the shift2 code to if($seq['shift2'][$i] < 7) $shift2 = ($x % 2 == 0) ? 2 : 1; // alternate between 2 and 1 elseif($seq['shift2'][$i] == 7) { $shift2 = 'Off'; $x++; // increment x for each 'Off' } The result become 2 all.. where i put variable x? before this? for($i = 0; $i < 31; $i++) Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 8, 2014 Share Posted January 8, 2014 (edited) where i put variable x? before this? for($i = 0; $i < 31; $i++) Yes, the start of the for loop should read $x = 0; for($i = 0; $i < 31; $i++) Edit. Ohh shoot. $seq['shift2'] should of been $seq['kerja2'] if($seq['kerja2'][$i] < 7) $shift2 = ($x % 2 == 0) ? 2 : 1; // alternate between 2 and 1 elseif($seq['kerja2'][$i] == 7) { $shift2 = 'Off'; $x++; // increment x for each 'Off' } Edited January 8, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
deib97 Posted January 9, 2014 Author Share Posted January 9, 2014 Yes, done. The result start shift from 2, because $shift2 = ($x % 2 == 0) ? 2 : 1; // alternate between 2 and 1 And How to determine start shift whitout modulus, But I can change start shift 1 or 2, maybe need variable or somethings? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 9, 2014 Share Posted January 9, 2014 (edited) But I can change start shift 1 or 2, maybe need variable or somethings? How do you know when to start shift at 1 or 2? Edited January 9, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
deib97 Posted January 9, 2014 Author Share Posted January 9, 2014 With form input shift_start (dropdown) : 1 or 2 or 1 and 2 (like your code is answered if 1 and 2 combination) Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 9, 2014 Share Posted January 9, 2014 Not quite understanding your question. Are you wanting to make a dropdown and then chose which shift to display, eg if you select shift 1 you'd see Shift 1 2 2 2 2 2 2 2 Off 1 1 1 1 1 1 2 2 2 2 2 2 2 Off 1 1 1 1 1 1 2 2 If you select shift 2 then shift2 will be shown Shift2 2 2 Off 1 1 1 1 1 1 Off 2 2 2 2 2 2 Off 1 1 1 1 1 1 Off 2 2 2 2 2 2 Off If you select both then shift 1 and 2 will be shown Shift 1 2 2 2 2 2 2 2 Off 1 1 1 1 1 1 2 2 2 2 2 2 2 Off 1 1 1 1 1 1 2 2 Shift2 2 2 Off 1 1 1 1 1 1 Off 2 2 2 2 2 2 Off 1 1 1 1 1 1 Off 2 2 2 2 2 2 Off Quote Link to comment Share on other sites More sharing options...
deib97 Posted January 10, 2014 Author Share Posted January 10, 2014 Hmm,,, please, you can try this bro.. this tes.php <form action="shift.php" method="POST"> Tanggal : <input type="text" name="tanggal" value="" /> <!--date value ex: 2014-01-01--> <br> Hari ke : <select name="hari_ke"> <!--this for Lembur, i was change to variable, thanks for support // array for lembur on line 4 $seq['lembur'] = array(); $seq['lembur'][0] = 4, if selected 4 ; $seq['kerja2'] = array(); $seq['kerja2'][0] = 4, if selected 4 ; --> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> </select><br> Shift : <select name="shift"> <option value="1">1</option> <!--if this value selected, "shift mulai" must default to 1, patern pola shift 7/1 1 1 1 Off 1 1 1 1 1 1 Off 1 1 1 1 1 1 Off 1 1 1 1 1 1 Off 1 1 1 1 1 1 --> <option value="2">2</option> <!--if this value selected, "shift mulai" must default to 1, patern pola shift 7/1 2 2 2 Off 2 2 2 2 2 2 Off 2 2 2 2 2 2 Off 2 2 2 2 2 2 Off 2 2 2 2 2 2 --> <option value="3">1 dan 2</option> <!--if this value selected, "shift mulai", can be change to 1 or 2 in shift mulai --> </select> *if selected 1, must be default or automatically "shift mulai" is 1,<br> *if selected 2, must be default or automatically "shift mulai" is 2,<br> *if selected 1 and 2, must be can change "shift mulai" to 1 or 2 <br> Shift mulai : <select name="shift_mulai"> <option>1</option> <!--if this value selected, based on shift value 3 => 1 dan 2, shift start for 1 and continue to 2, patern pola shift 7/1 1 1 1 Off 2 2 2 2 2 2 Off 1 1 1 1 1 1 Off 2 2 2 2 2 2 Off 1 1 1 1 1 1 --> <option>2</option> <!--if this value selected, based on shift value 3 => 1 dan 2, shift start for 2 and continue to 1, patern pola shift 7/1 2 2 2 Off 1 1 1 1 1 1 Off 2 2 2 2 2 2 Off 1 1 1 1 1 1 Off 2 2 2 2 2 2 --> </select> <br> Pola : <select name="pola_shift"> <option value="1">7/1</option> <option value="2">14/1</option> </select><br><input type="submit" value="submit" /> </form> and shift.php <?php $datei=$_POST["tanggal"]; $day_number=$_POST["hari_ke"]; $shift=$_POST["shift"]; $shift_start=$_POST["shift_mulai"]; $pattern_shift=$_POST["pola_shift"]; if ($pattern_shift == 1) { $hpattern=7; } else { $hpattern=14; } date_default_timezone_set('UTC'); $hasil = explode("-", $datei); $hasil[2]; // day $hasil[1]; // month $d1=$hasil[2]; $m1=$hasil[1]; $y1=$hasil[0]; $num = cal_days_in_month(CAL_GREGORIAN, $m1, $y1); $d2 = $num; /* $m2 = $m1; $y2 = $y1; $start_date = array($y1,$m1,$d1); $implode1 = implode("-", $start_date); $end_date = array($y2,$m2,$d2); $implode2 = implode("-", $end_date); while(strtotime($implode1) <= strtotime($implode2)) { echo "$implode1 - ? <br>"; $implode1 = date ("Y-m-d", strtotime("+1 day", strtotime($implode1))); } */ // array for lembur sequane $seq['lembur'] = array(); $seq['lembur'][0] = $day_number; // array for kerja sequance $seq['kerja'] = array(); $seq['kerja'][0] = $day_number; //$seq['kerja2'] = array(); //$seq['kerja2'][0] = 4; // array for shift sequance $seq['shift'] = array(); //$seq['shift2'] = array(); // the max number foreach sequance , 'kerja2' => 7 $seqances = array('lembur' => 7, 'kerja' => $hpattern); $x=0; // generate the sequances for($i = 0; $i < $d2; $i++) { // generate the number sequances for lembur and kerja foreach($seqances as $key => $maxSeq) { // if current postion is set, then skip it if(isset($seq[$key][$i])) continue; // get previous number in current squance $num = $seq[$key][$i - 1] + 1; // is number greater than the max sequance number // reset sequance if($num > $maxSeq) $num -= $maxSeq; // add number to current sequance $seq[$key][$i] = $num; } if ($hpattern== 7 ){ if($seq['kerja'][$i] < 7) $shift = $shift_start; else $shift = 'Off'; } else if ($hpattern=14) { // the shift sequance if($seq['kerja'][$i] < 7) $shift = $shift_start; elseif($seq['kerja'][$i] < 14) $shift = $shift_start; else $shift = 'Off'; } // if($seq['kerja2'][$i] < 7) // $shift2 = ($x % 2 == 0) ? 2 : 1; // alternate between 2 and 1 // elseif($seq['kerja2'][$i] == 7) { // $shift2 = 'Off'; // $x++; // increment x for each 'Off' // } $seq['shift'][$i] = $shift; // $seq['shift2'][$i] = $shift2; } ?> <style> th { color: red; width: 20px;} td { text-align: center; } </style> <table> <tr> <th>Tanggal</th> <th><?php echo implode('</th><th>', range(1, $d2)); ?></th> </tr> <?php foreach($seq as $key => $sequance): ?> <tr> <td><?php echo ucwords($key) ?></td> <td><?php echo implode('</td><td>', $sequance); ?></td> </tr> <?php endforeach; ?> </table> with your code, : $x=0 before looping if($seq['kerja2'][$i] < 7) $shift2 = ($x % 2 == 0) ? 2 : 1; // alternate between 2 and 1 elseif($seq['kerja2'][$i] == 7) { $shift2 = 'Off'; $x++; // increment x for each 'Off' } The result will be shown : Shift2 2 2 Off 1 1 1 1 1 1 Off 2 2 2 2 2 2 Off 1 1 1 1 1 1 Off 2 2 2 2 2 2 Off And i want to change shift start to 1 and continue to 2; or shift start to 2 and continue to 1 Maybe with a bit of explanation may help you to help me. thanks im stuck Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 10, 2014 Share Posted January 10, 2014 Try these attached files. I changed the 1 dan 2 option into a checkbox. You can select how the shift starts from a single drop down menu. shift.php shift_form.php Quote Link to comment Share on other sites More sharing options...
deib97 Posted January 10, 2014 Author Share Posted January 10, 2014 Great answer... awesome, So, I would to say thank... cool code... Quote Link to comment Share on other sites More sharing options...
deib97 Posted January 12, 2014 Author Share Posted January 12, 2014 Based on excel for pattern 14/1, Shift 1 2 2 2 2 2 2 2 Off 1 1 1 1 1 1 2 2 2 2 2 2 2 Off 1 1 1 1 1 1 2 2 How to choose shift_start 1 or 2?? I change and modify your code become to : 1 1 1 1 1 1 Off Off Off Off Off Off Off Off 2 2 2 2 2 2 Off Off Off Off Off Off Off Off 1 1 1 Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted January 12, 2014 Solution Share Posted January 12, 2014 Try <?php $datei = $_POST["tanggal"]; $day_number = $_POST["hari_ke"]; $shift_start = $_POST["shift_mulai"]; $shift_1dan2 = isset($_POST["shiftAlt"]) ? true : false; $pattern_shift = $_POST["pola_shift"]; if ($pattern_shift == 1) { $hpattern = 7; } else { $hpattern = 14; } $shiftStart = array( 1, // first shift value 2 // secound shift value ); if($shift_start == 2) $shiftStart = array_reverse($shiftStart); // reverse the shift values date_default_timezone_set('UTC'); $hasil = explode("-", $datei); $d1=$hasil[2]; // day $m1=$hasil[1]; // month $y1=$hasil[0]; $num = cal_days_in_month(CAL_GREGORIAN, $m1, $y1); $d2 = $num; // array for lembur sequane $seq['lembur'] = array(); $seq['lembur'][0] = $day_number; // array for kerja sequance $seq['kerja'] = array(); $seq['kerja'][0] = $day_number; //$seq['kerja2'] = array(); //$seq['kerja2'][0] = 4; // array for shift sequance $seq['shift'] = array(); //$seq['shift2'] = array(); // the max number foreach sequance , 'kerja2' => 7 $seqances = array('lembur' => 7, 'kerja' => $hpattern); $x = 0; // generate the sequances for($i = 0; $i < $d2; $i++) { // generate the number sequances for lembur and kerja foreach($seqances as $key => $maxSeq) { // if current postion is set, then skip it if(isset($seq[$key][$i])) continue; // get previous number in current squance $num = $seq[$key][$i - 1] + 1; // is number greater than the max sequance number // reset sequance if($num > $maxSeq) $num -= $maxSeq; // add number to current sequance $seq[$key][$i] = $num; } $kerjaValue = $seq['kerja'][$i]; // default shift value to off $shiftValue = 'Off'; // if shift not with 1 dan 2 if(!$shift_1dan2) { // Shift pattern 14/1 if($hpattern == 14 && $kerjaValue < 14) $shiftValue = $shiftStart[1]; if($kerjaValue < 7) $shiftValue = $shiftStart[0]; } // shift 1 dan 2 elseif($shift_1dan2) { if($kerjaValue < $hpattern) $shiftValue = ($x % 2 == 0) ? $shiftStart[0] : $shiftStart[1]; // alternate between shift values, when x is dividable by 2 if($shiftValue == 'Off') $x++; // increment x when shift is Off } $seq['shift'][$i] = $shiftValue; } ?> <style> th { color: red; width: 20px;} td { text-align: center; } </style> <table> <tr> <th>Tanggal</th> <th><?php echo implode('</th><th>', range(1, $d2)); ?></th> </tr> <?php foreach($seq as $key => $sequance): ?> <tr> <td><?php echo ucwords($key) ?></td> <td><?php echo implode('</td><td>', $sequance); ?></td> </tr> <?php endforeach; ?> </table> Quote Link to comment Share on other sites More sharing options...
deib97 Posted January 13, 2014 Author Share Posted January 13, 2014 Finally complete, thanks advance Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.