【问题标题】:how to get the values in array and how to insert in database using codeiniter如何获取数组中的值以及如何使用 codeigniter 在数据库中插入
【发布时间】:2017-04-18 11:21:53
【问题描述】:

这里我有一个数组,在这个数组中我还有一个数组名studentAbsentId,现在我想在数据库中插入studentAbsentId,就像这里studentAbsentId 2 和studentAbsentId 2 不存在所以我需要插入两行

我更新的代码print_r($params);

 Array
(
    [studentAbsentId] => Array
        (
            [0] => 2
            [1] => 3
        )

    [schoolId] => 2
    [classId] => 1
    [sectionId] => 1
    [studentAbsentDate] => 2017-04-18
    [reg_date] => 2017-04-18 05:20:17
    [created_by] => 1
)

现在我只想带studentAbsentId,怎么办?

预期结果

2

3

【问题讨论】:

  • 那么当你运行这段代码时发生了什么?你看到任何错误吗?描述并提及您的问题
  • 请检查我更新的代码

标签: php json codeigniter model


【解决方案1】:

试试这个有帮助:)

<?php

$params=array(
        'studentAbsentId' => array
            ( 2,3
            ),
        'studentAbsentDate' => '2017-04-18',
        'reg_date' => '2017-04-18 04:41:21',
        'created_by' => '1',
    );


foreach($params['studentAbsentId'] as $ff)
{
        $atnEntry = array(
        "studentAbsentId" => $ff,
        "studentAbsentDate" =>$params['studentAbsentDate'],
        "morning" => "1"

        );

        $this->db->insert("student_absent_list" , $atnEntry);
}        
        $return = array("status" => "Success" );
        echo json_encode($return);  

?>

【讨论】:

    【解决方案2】:

    试试这个

    $test = array('studentAbsentId' => array('2','3'),'studentAbsentDate' => '2017-04-18','reg_date' => '2017-04-18 04:41:21','created_by' => '1');
    
        foreach($test['studentAbsentId'] AS $tet)
        {
            $array_val = array('studentAbsentId' => $tet,'studentAbsentDate' => $test['studentAbsentDate'],'created_by' => $test['created_by']);
    
            // echo "<pre>";print_r($array_val);
    
            $this->db->insert('table_name',$array_val);
        }
    

    【讨论】:

      猜你喜欢
      • 2021-02-07
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 2015-09-14
      • 2018-11-24
      • 2016-04-10
      • 2016-08-25
      • 1970-01-01
      相关资源
      最近更新 更多