doujia2386 2016-12-08 11:58
浏览 27
已采纳

php脚本生成计划

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
" ;   
        echo  $input[$rand_keys[1]] ."15H-23H
" ;  
        echo  $input[$rand_keys[2]] ."23H-7H
" ;           

    }
    echo "</td>";
}   
?>
  • 写回答

1条回答 默认 最新

  • duanbei1903 2016-12-08 13:11
    关注

    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>
    ";
    
        echo  $input[$rand_keys[0]] ." 7H-15H
    " ;   
        echo  $input[$rand_keys[1]] ." 15H-23H
    " ;  
        echo  $input[$rand_keys[2]] ." 23H-7H
    " ;           
    
        echo "</td>
    ";
    }
    

    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>
    ";
        shuffle($input);
        echo  $input[0] ." 7H-15H
    " ;   
        echo  $input[1] ." 15H-23H
    " ;  
        echo  $input[2] ." 23H-7H
    " ;           
    
        echo "</td>
    ";
    }
    

    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>
    ";
        shuffle($input);
        while ($input[0] == $last_shift) {
            shuffle($input);
        }
        echo  $input[0] ." 7H-15H
    " ;   
        echo  $input[1] ." 15H-23H
    " ;  
        echo  $input[2] ." 23H-7H
    " ;        
    
        $last_shift = $input[2];
        echo "</td>
    ";
    }
    

    DEMO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?