php - 在运行时为 php 对象分配名称
问题描述
我想根据所选月份选择员工的出勤详细信息。我想使用与月份相同的对象名称存储每个月的数据。我不明白如何在运行时分配名称。这是我在下面给出的代码
for ($i=0; $i < $totalmonth; $i++) {
$query="SELECT attendance FROM attendance
WHERE month='$m' && year='$y' && dept='$dept' && location='$loc'";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$dbdata = array();
while ( $row = $result->fetch_assoc())
{
array_push($dbdata, $row);
}
}
else
{
echo 'Data Not Found';
}
echo json_encode(array('data' => $dbdata));
}
解决方案
使用关联数组将月份名称指定为代码中的键,如下所示;
while ( $row = $result->fetch_assoc())
{
$dbdata[$m]=$row;
}
推荐阅读
- gradle - Gradle,如何通过设置插件共享目录
- c# - RedirectToAction 重定向到错误的用户 ID
- javascript - javascript insertBefore 在 addEventListener 上只有一次
- python - Pandas csv.read 将 csv 列作为字符串而不是整数提取,并将它们作为单个参数输入到数据库的第一列中
- reactjs - 在 ReactJS 中,我们真的可以只使用 useEffect() 和 useState() 来做一些概念验证工作吗?useEffect() 的任何替代方法?
- php - 卡在 laravel,试图将 id 存储在变量中
- swift - 如何将 UIImageView 固定到顶部而不拉伸它?
- javascript - Fullcalendar 没有加载我的 json 来显示事件
- javascript - 如何将两个数组组合成一个二维数组?
- encryption - 加密具有 2 个或少数值的多个字符串