dqrdlqpo775594 2017-03-15 17:51
浏览 48
已采纳

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

I am creating a program that takes current made shifts from a table and gives users shifts in another table called schedule based on their staffID is which is from the userStaff table.

I am simply looking for it to loop through each staff member on each date and give them a random shift from the the preset shifts in the table.

My first theory was, iterate through the userStaff table where a random integer from the amount of shifts available for the shift position and then choose one and assign it on the schedule table. However, it keep coming up with no values and only looking through one staffID.

I couldn't find anything that was as specific as this. The error I am having is: no values are assigned and only one user ID is iterated for?

UserStaff Table Example:

ID - 1, 2, 3

Username - jenk3194, jake1233, rodger1293

Position - BM, CT, CM

Shifts Table Example:

ID - 1, 2, 3

shiftstart - 09:00:00, 12:00:00, 16:00:00

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

shiftperson - BM, CT, CM

Schedule Table Example:

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:00

Here is my full current code:

$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条回答 默认 最新

  • dqunzip3183 2017-03-20 23:00
    关注

    This code here is the working code that includes an entire year of data. Therefore 2190 results for all 6 users in my database. I have 17 results in my preset shift table for each position therefore it loops through all of them.

    #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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?