Jump to content

Make looping increment number


deib97
Go to solution Solved by Ch0cu3r,

Recommended Posts

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 by Ch0cu3r
Link to comment
Share on other sites

Expansion case for schedule shift.

https://dl.dropboxusercontent.com/s/2ksm9i7mrk91dd0/simulasi%20Roster.xls?dl=1&token_hash=AAGI7XXQjXJXSWQs7NQMbUTCBgOoCgn9xOKZnaYRbFYdsA

 

On this excel

  1. Hari ke u/ jadwal kerja is same solution from "Ch0cu3r", done
  2. 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..

Link to comment
Share on other sites


<?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>

 

Link to comment
Share on other sites

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.

inputan.jpg?dl=1&token_hash=AAFJsoZjMRyn

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 by deib97
Link to comment
Share on other sites

 

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++)

Link to comment
Share on other sites

 

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 by Ch0cu3r
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 :

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Solution

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>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.