【问题标题】:LAMP attendance systemLAMP考勤系统
【发布时间】:2013-08-23 09:06:25
【问题描述】:

我只是无法找到解决这个问题的正确方法,我有两个数组。 第一个数组 $attendance 具有以下键和值

Array
(
[attendance] => Array
    (
        [1] => 1
        [2] => 1
        [3] => 0
        [4] => 1
        [5] => 1
        [6] => 0
        [7] => 0
    )

如果选中的值为 1,则来自复选框,否则值为 0 第二个数组是$remark

[remark] => Array
    (
        [1] =>
        [2] => 
        [3] =>  'sick'
        [4] => 
        [5] => 
        [6] =>  'leave'
        [7] =>  'On assignment'

    ) 

现在这就是键 1-7 代表的意思,脚本用于员工出勤,键 1-7 是我数据库中员工表中的员工 ID。

现在我想要实现的是以这样的方式连接数组

Array
(
[0] => Array
    (
        [employeeID] => 7
        [attendance] => 0
        [remark] => 'On assignment'
    )

[1] => Array
    (
        [employeeID] => 6
        [attendance] => 0
        [remark] => 'leave'
    )

[2] => Array
    (
        [employeeID] => 5
        [attendance] => 1
        [remark] => 
    )

//and so on
)

我正在使用 Codeigniter 如果我能够连接它,我也很想知道如何将多个数据插入到如下所示的员工表中,

employee's table
employeeID | date | status | remarks

我计划使用 CURDATE() 的日期,然后状态将保持 0 或 1 从出席 再次:备注和考勤数组中1-7的键是employeeID

更新!! 这是我尝试过但没有奏效的方法。

$att = $_POST['attendance'];
$remarks = $_POST['remark'];

foreach($att as $employee_id=>$status)
        {
            $x=0;
            $employee[$x]['employee_id']=$employee_id;
            $employee[$x]['status']=$status;


            foreach($remarks as $employee_id=>$remark)
            {

                $employee[$x]['remark']=$remark;
                $x++;
            }
       }  

【问题讨论】:

  • 到目前为止你有什么?这是一些相当简单的数组操作。
  • @FritsvanCampen 刚刚添加了它

标签: php mysql codeigniter multidimensional-array


【解决方案1】:
$attendance = $_POST['attendance'];
$remarks = $_POST['remark'];

$collect = array();
foreach ($attendance as $employeeID => $attending) {
    $collect[] = array(
        "employeeID" => $employeeID,
        "attendance" => $attending,
        "remark" => isset($remarks[$employeeID]) ? $remarks[$employeeID] : ""
    );
}

$values = array();
foreach ($collect as $entry) {
    $values[] = "(" . ((int) $entry["employeeID"]) . ",
        CURDATE(),
        " . ((int) $entry["attendance"]) . ",
        '" . sql_escape($entry["remark"]) . "'
    )";
}
$query = "INSERT INTO `attendance`
    (`employeeID`, `date`, `status`, `remarks`)
    VALUES
    (" . implode(",", $values) . ")";

确保将sql_escape 替换为适当的转义函数。如果您使用的是 PDO,请使用它。

【讨论】:

    【解决方案2】:

    说实话,很简单......

    $numOfEmployees = 8;
    
    $concatArray = array();
    
    foreach($attendance as $k => $v)
    {
         $concatArray[] = array("employeeID" => $k, "attendance" => $v, "remark" => $remark[$k];
    }
    

    如果你的出勤表是 SQL 之类的,你可以在上面提到的 concatArray 上使用foreach 来处理它:

    foreach($concatArray as $key => $value)
    {
         $remark = $value['remark'];
         // sanitize remark for database
         $query = "INSERT INTO employees (ID, Date, Status, Remarks) VALUES ({$value['employeeID']}, NOW(), {$value['attendance']}, '{$value['remark']}');";
         // perform the actual query
    }
    

    请记住,这是一个非常笼统的例子,对数据库做了很多假设。

    正如 Frits 所指出的,应该对备注字段进行清理,可能对 MySQLi 使用 mysqli_real_escape_string 或对 PDO 使用 quote - 取决于您使用的内容。

    【讨论】:

    • 无法保证所有$i 都是员工ID。如您所见,没有 ID 为 0 的员工。只需对$attendance 执行foreach 即可获得实际的员工ID。另外,如果我的评论是He's late again,那将破坏您的查询;)
    • 谢谢,我已尽力将您的建议包含在答案中:)
    猜你喜欢
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 2019-10-12
    • 2020-11-11
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多