0

I want to distribute some personnel in a week every day there are 3 times 7h-15h 15-23h 23h-7h .

The problem that I don't want a person to show more than one time in a day. I want to distribute personnel list to the day of week I try this:

<?php
$input = array("Name1","Name2",  "Name3","Name4");
$rand_keys = array_rand($input, 3);


?>
<table  border=1>

<tr>

<th> Samedi </th> <th> Dimanche </th> <th> Lundi </th> <th> Mardi </th> <th> Merecredi </th> <th> Jeudi </th> <th> Vendredi </th>
</tr>
<tr>
<?php
$j=1;
for($i=0;$i<7;$i++){

    echo "<td>";
    for($k=0;$k<3;$k++){

        echo  $input[$rand_keys[0]] ."7H-15H\n" ;   
        echo  $input[$rand_keys[1]] ."15H-23H\n" ;  
        echo  $input[$rand_keys[2]] ."23H-7H\n" ;           

    }
    echo "</td>";
}   
?>
6
  • What's a schudle? Did you mean schedule?
    – Barmar
    Commented Dec 8, 2016 at 12:27
  • yes schedule but in reality I need just to distribute some personne in a week like what what I said in My question Commented Dec 8, 2016 at 12:29
  • What's wrong with the code you have? Maybe you need to call array_rand() each time through the loop, so you get different people on each day?
    – Barmar
    Commented Dec 8, 2016 at 12:34
  • the problem that I n=don't want a person to show more than one time in a day Commented Dec 8, 2016 at 12:50
  • I don't see how your code would do that. It puts a different person in each shift on the day.
    – Barmar
    Commented Dec 8, 2016 at 12:53

1 Answer 1

0

You don't need the second loop for ($k = 0; $k < 3; $k++), because you're already showing each shift with the 3 echo statements.

for($i=0;$i<7;$i++){

    echo "<td>\n";

    echo  $input[$rand_keys[0]] ." 7H-15H\n" ;   
    echo  $input[$rand_keys[1]] ." 15H-23H\n" ;  
    echo  $input[$rand_keys[2]] ." 23H-7H\n" ;           

    echo "</td>\n";
}

You're also using the same schedule every day of the week. So one of the people is never being scheduled for the entire week. You probably should shuffle the array each day, not just at the beginning of the script.

for($i=0;$i<7;$i++){

    echo "<td>\n";
    shuffle($input);
    echo  $input[0] ." 7H-15H\n" ;   
    echo  $input[1] ." 15H-23H\n" ;  
    echo  $input[2] ." 23H-7H\n" ;           

    echo "</td>\n";
}

DEMO

To keep a person from working two shifts in a row if they're assigned to 23H-7H and then 7H-15H the next day, you can set a variable to that person, and check if the shuffle put them first.

$last_shift = $input[array_rand($input)];

for($i=0;$i<7;$i++){

    echo "<td>\n";
    shuffle($input);
    while ($input[0] == $last_shift) {
        shuffle($input);
    }
    echo  $input[0] ." 7H-15H\n" ;   
    echo  $input[1] ." 15H-23H\n" ;  
    echo  $input[2] ." 23H-7H\n" ;        

    $last_shift = $input[2];
    echo "</td>\n";
}

DEMO

4
  • Very Nice Thank u so Much Barmer I need just last little thing I want to be sure that the person not continu working like if one work yesterday 23h-7h ha can not start next day in 7h Commented Dec 8, 2016 at 13:23
  • I added a version that checks for this and reshuffles.
    – Barmar
    Commented Dec 8, 2016 at 13:31
  • You are realy clever Just last thing I want every one to get just one day per week for rest even If I add two person on same HOur like Sidi$Ahmed 7h-15H Commented Dec 8, 2016 at 13:34
  • Sorry, I'm done, I'm not going to write the whole application for you.
    – Barmar
    Commented Dec 8, 2016 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.