如何遍历SQL表并从预定义的表中将值分配给另一个SQL表?

时间:2017-03-15 17:51:33

标签: php mysql

我正在创建一个程序,该程序从表中获取当前的转换,并根据其staffID来自userStaff表,让用户在另一个称为schedule的表中进行转换。

我只是想让它在每个日期循环遍历每个工作人员,并从表格中的预设班次中随机转移。

我的第一个理论是,迭代userStaff表,其中从换档位置可用的班次数量中随机整数,然后选择一个并在计划表上分配。但是,它不断提出任何值,只查看一个staffID。

我找不到任何具体的东西。我遇到的错误是:没有赋值,只有一个用户ID被迭代?

UserStaff表示例:

ID - 1,2,3

用户名 - jenk3194,jake1233,rodger1293

职位 - BM,CT,CM

转移表格示例:

ID - 1,2,3

shiftstart - 09:00:00,12:00:00,1:00:00

shiftend - 17:00:00,21:00:00,00:00:00

shiftperson - BM,CT,CM

日程表示例:

ID - 1,2,3

staffID - 1,2,3

cdate - 2017-01-01,2017-01-01,2017-01-01

starttime - 12:00:00,16:00:00,09:00:00

endtime - 21:00:00,00:00:00,17:00

以下是我当前的完整代码:

$sql = "SELECT * FROM userStaff";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) 
{
// output data of each row
while($row = mysqli_fetch_assoc($result)) 
{
    $date = "2017-03-13";
    #$date = date("Y-m-d");

    $begin = new DateTime($date);
    $end = new DateTime(date('Y-m-d', strtotime($date . '+ 7 day'))); // you can change + 1 year to what you need

    $interval = new DateInterval('P1D'); // one day
    //$interval = new DateInterval('P1W'); // one week

    $daterange = new DatePeriod($begin, $interval, $end);

    foreach ($daterange as $date) { // loop through dates
        $fDate = $date->format("Y-m-d");
        $rowID = $row['id'];
        $sql = "SELECT position FROM userStaff WHERE id = '$rowID'";
        echo $sql;
        $result = mysqli_query($conn, $sql);
        if (mysqli_num_rows($result) > 0 )
        {
            $int = mt_rand(1, 3);
            $sql = "SELECT * FROM shifts WHERE id = $int AND shiftperson = '".  $row['position'] . "'";
            $result = mysqli_query($conn, $sql);
            if (mysqli_num_rows($result) > 0)
            {
                while ($row = mysqli_fetch_array($result)) {
                    $shiftSt = $row['shiftstart'];
                    $shiftEn = $row['shiftend'];
                    $sql = "INSERT INTO schedule (staffID, cdate, starttime, endtime) VALUES ('$rowID','$fDate','$shiftSt','$shiftEn')";
                    echo $sql . "<br>";
                    if (mysqli_query($conn, $sql)) 
                    {
                        echo "<br>Success<br>";
                    }
                    else
                    {
                        echo "<br>Failure<br>";
                    }
                }
            }
        }
        else
        {
            echo "Failed";
            }
        }
   }
} 
else 
{
    echo "<p>No Results</p>";
}

1 个答案:

答案 0 :(得分:0)

此处的代码是包含整年数据的工作代码。因此,我的数据库中的所有6个用户的结果为2190。我在每个位置的预设换档表中有17个结果,因此它遍历所有位置。

#echo "User Schedule Populated";

$sql="SELECT id FROM userStaff";

$result = mysqli_query($conn, $sql); 

$allID = Array();

while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) 
{
    $allID[] = $row['id'];

}

foreach($allID as $idR)
{
    $date = "2017-01-01";

    $rowID = $idR;
    echo "RowID: " . $rowID . "<br>";
    echo "Count 1: " . $counter;
    $begin = new DateTime($date);
    $end = new DateTime(date('Y-m-d', strtotime($date . '+ 1 year')));

    $interval = new DateInterval('P1D'); // one day

    $daterange = new DatePeriod($begin, $interval, $end);
    foreach ($daterange as $date)
    {   
        $fDate = $date->format("Y-m-d");
        $counter ++;
        echo "<br> Count 1: " . $counter . "<br>";
        echo "<br>";
        $sql = "SELECT position FROM userStaff WHERE id ='$rowID'";
        echo $sql;

        $result = mysqli_query($conn, $sql);

        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        $int = mt_rand(1,17);
        echo "<br>";
        $sql = "SELECT * FROM shifts WHERE shiftnum = '$int' AND shiftperson = '" . $row['position'] . "'";
        $result = mysqli_query($conn, $sql);
        echo $sql;
        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        $shiftSt = $row['shiftstart'];
        echo "<br> Shift St: " . $shiftSt;
        $shiftEn = $row['shiftend'];
        echo "<br> Shift En: " . $shiftEn;
        $sql = "INSERT INTO schedule (staffID, cdate, starttime, endtime) VALUES ('$rowID','$fDate','$shiftSt','$shiftEn')";
        echo "<br> Insert: " . $sql;
        mysqli_query($conn, $sql);

    }

}
die();