如何将第一个表中的数据插入到第二个表中,就像这个例子一样

时间:2014-10-15 15:17:29

标签: php mysql codeigniter

我使用的是codeigniter和mysql,我有两张桌子。 第一个表名“checkinout”: 第二个表名“check_times”:

表名: “checkinout”

----------------------------------------------------------------------------
user_id  | date         | time
----------------------------------------------------------------------------
 3       |  2014-09-30  | 05:40:35,06:40:35,09:40:35,12:40:35
 3       |  2014-10-01  | 05:40:35,10:30:35
 2       |  2014-11-22  | 07:00:35

我想将第一个表中的数据插入到第二个表中 是这样的: 表名: “check_times”

------------------------------------------------------------------------------
user_id  | date         | in_1       | out_1      | in_2       | out_2 
------------------------------------------------------------------------------
 3       |  2014-09-30  |  05:40:35  |  06:40:35  |  09:40:35  |  12:40:35 
 3       |  2014-10-01  |  05:40:35  |  10:30:35  |            |
 2       |  2014-11-22  |  07:00:35  |            |            |

=============================================== ===============================
我尝试使用explode()函数,但它显示每个空值的错误消息。

我的型号名称是“test_model”
这是我的模特函数

public function get_data()
{
    $query = $this->db->get('checkinout')->result();
    foreach ($query as $result)
    {
        if($result->check_time)
        {
            $result->check_time = explode(",", $result->check_time);
        }
    }
    return $query;
}

我的控制器名称是“test”
这是我的控制器功能

public function show()
{
    $query = $this->test_model->get_data();
    foreach ($query as $row)
    {
        $data['results'] = array
        (
            'userid' => $row->userid ,
            'check_date' => $row->check_date , 
            'in_1' =>$row->check_time[0] ,
            'out_1' =>$row->check_time[1] ,
            'in_2' =>$row->check_time[2] ,
            'out_2' =>$row->check_time[3]
        );
        $this->load->view('test_view',$data);
    }
}

我的观点名称是“test_view”

echo "<pre>";
print_r($results);

结果是

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 2

Filename: controllers/test.php

Line Number: 50

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 3

Filename: controllers/test.php

Line Number: 51
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: controllers/test.php

Line Number: 49
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 2

Filename: controllers/test.php

Line Number: 50
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 3

Filename: controllers/test.php

Line Number: 51


Array
(
[userid] => 3
[check_date] => 2014-09-30
[in_1] => 12:40:35
[out_1] => 09:40:35
[in_2] => 06:40:35
[out_2] => 05:40:35
)
Array
(
[userid] => 3
[check_date] => 2014-10-01
[in_1] => 10:30:35
[out_1] => 05:40:35
[in_2] => 
[out_2] => 
)
Array
(
[userid] => 2
[check_date] => 2014-11-22
[in_1] => 07:00:35
[out_1] => 
[in_2] => 
[out_2] => 

修改后的我的控制器

public function show()
{
    $query=$this->test_model->get_data();
    foreach ($query as $row)
    {
        $data['results'] = array();
        $data['results']['userid']= $row->userid ;
        $data['results']['check_date'] = $row->check_date ;

        if(isset($row->check_time[0]))
        {$data['results']['in_1']  = $row->check_time[0] ;}
        else{$data['results']['in_1']  = '00:00:00' ;}

        if(isset($row->check_time[1]))
        {$data['results']['out_1'] = $row->check_time[1] ;}
        else{$data['results']['out_1']  = '00:00:00' ;}

        if(isset($row->check_time[2]))
        {$data['results']['in_2']  = $row->check_time[2] ;}
        else{$data['results']['in_2']  = '00:00:00' ;}

        if(isset($row->check_time[3]))
        {$data['results']['out_2'] = $row->check_time[3] ;}
        else{$data['results']['out_2']  = '00:00:00' ;}

        $this->load->view('test_view',$data);
    }
}

谢谢大家,现在很好

1 个答案:

答案 0 :(得分:0)

time表中获取字段checkinout,使用explode()函数并准备check_times上的插入