[英]How to iterate through an SQL table and assign values to another SQL table from a predefined table?
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. 我正在创建一个程序,该程序从一个表中获取当前的轮班制,并根据其userIDaff表中的staffID为用户提供另一个名为schedule的表中的轮班制。
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. 我的第一个理论是遍历userStaff表,在该表中从可用于班次位置的班次数量中随机抽取一个整数,然后选择一个,并将其分配到计划表中。 However, it keep coming up with no values and only looking through one staffID.
但是,它一直没有提供任何值,而只是通过一个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?
我遇到的错误是:没有分配值,并且仅迭代一个用户ID?
UserStaff Table Example: UserStaff表示例:
ID - 1, 2, 3 ID-1,2,3
Username - jenk3194, jake1233, rodger1293 用户名-jenk3194,jake1233,rodger1293
Position - BM, CT, CM 位置-BM,CT,CM
Shifts Table Example: 班次表示例:
ID - 1, 2, 3 ID-1,2,3
shiftstart - 09:00:00, 12:00:00, 16:00:00 shiftstart-09:00:00、12:00:00、16:00:00
shiftend - 17:00:00, 21:00:00, 00:00:00 shiftend-17:00:00、21:00:00、00:00:00
shiftperson - BM, CT, CM 移位人员-BM,CT,CM
Schedule Table Example: 进度表示例:
ID - 1, 2, 3 ID-1,2,3
staffID - 1, 2, 3 staffID-1,2,3
cdate - 2017-01-01, 2017-01-01, 2017-01-01 日期-2017-01-01,2017-01-01,2017-01-01
starttime - 12:00:00, 16:00:00, 09:00:00 开始时间-12:00:00、16:00:00、09:00:00
endtime - 21:00:00, 00:00:00, 17:00:00 结束时间-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>";
}
This code here is the working code that includes an entire year of data. 此处的代码是包含全年数据的工作代码。 Therefore 2190 results for all 6 users in my database.
因此,我数据库中的所有6位用户得到2190条结果。 I have 17 results in my preset shift table for each position therefore it loops through all of them.
我的预设移位表中的每个位置都有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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.