I am working on a project where I have to do a random planning of 144 lawyers. This planning is per month.So on every day of a month will have 3-4 lawyers. This planning will be made at the beginning on every month. This planning must be random and every people to be picked-up equally.
My idea is like this: first time I picked up four people each day([144/31]=4) and the remaining 20 to be planned on 01-20 May. There are 4 plans and every lawyer choose on which plan they will participate. In my database I store in a table every option. For example, if he opts for plan 1 ( called politie_parchet_all
), he will have 1 in table from database).
The problem is here ...This code run just the if
clause,and the remaining 24 people remain non-selected.I think don't exit from the if
clause and I don't know why?
$str_date ="01-".$luna."-".$an;
$start_date = strtotime($str_date);
$end_date = strtotime('+1 month',$start_date);
$number_days = date("t",$start_date);
$nr_lawyer =number_lawyer($planificare);
while ($nr_lawyer >0)
{
$nr_lawyer =number_lawyer($planificare);
if($nr_lawyer > $number_days)
{
$nr = intval($nr_lawyer/$number_days);
while ($start_date < $end_date)
{
$time=date('Y-m-d',$start_date);
for ($i=1; $i<=$nr; $i++)
{
$time=date('Y-m-d',$start_date);
selectare_avocat($planificare,$time);
}
$start_date = strtotime('+1 days',$start_date);
}
}
else if($nr_lawyer < $number_days)
{
$str_date ="01-".$luna."-".$an;
$start_date = strtotime($str_date);
$end_date = strtotime('+'.number_lawyer($planificare).' days',$start_date);
echo date('Y-m-d',$end_date);
while ($start_date < $end_date)
{
$time=date('Y-m-d',$start_date);
selectare_avocat($planificare,$time);
$start_date = strtotime('+1 days',$start_date);
}
}
$nr_lawyer = number_lawyer($planificare);
}
selectare_avocat
is a function that will select a random lawyer, store this random lawyer in the generare_liste
table and update select = 1
select = 0
means that he can be selected. If a lawyer is selected they will not be selected again until the all lawyers have been picked up.
selectare_avocat() function : <- this function work well.
function selectare_avocat($planificare,$time) {
global $con;
$sql3="SELECT avocat.id from avocat WHERE avocat.select=0 AND avocat.".$planificare." = 1 AND avocat.id NOT IN (SELECT id_avocat FROM generare_liste WHERE `data` LIKE '{$time}') ORDER BY RAND() LIMIT 1";
$result3 = mysqli_query($con,$sql3);
$row=mysqli_fetch_array($result3);
$id = $row['id'];
$sql4="INSERT INTO generare_liste(id,id_avocat,planificare,data)VALUES(null,".$id.",'{$planificare}','{$time}')";
mysqli_query($con,$sql4);
$sql2="UPDATE `avocat` SET `select`=1 WHERE id={$id}";
mysqli_query($con,$sql2);
}
The problem is that this script never ends and remains blocked just on the if
clause (because after running I have just 124 lawyers in generare_lista
table) and the remaining 20 lawyers that must be picked up with the else
clause.
my suggestion is:
get count of day in actual month => $number_days = date("t",$start_date); initialize a variable $cont = 1; create a empty array $list = array(); do a loop in your array of data until it's empty; do $list[$cont] = some random value from your array data, then remove it from array data; test if $cont is greater than $number_days, then turn it to 1 increment $cont++;
this routine fits for any count of lawyers.