简体   繁体   English

如何将数据从第一个表插入第二个表,就像这个例子

[英]how to insert the data from the first table to the second table to be like this example

I am using codeigniter and mysql, i have two tables . 我正在使用codeigniter和mysql,我有两个表。 the first table name "checkinout": the second table name "check_times": 第一个表名称“ checkinout”:第二个表名称“ check_times”:

table_name:"checkinout" table_name:“签到”

----------------------------------------------------------------------------
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

i want to insert the data from the first table to the second table to be like this: table_name:"check_times" 我想将数据从第一个表插入到第二个表中,如下所示:table_name:“ 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  |            |            |

============================================================================== ================================================== ===========================
I tried using explode() function but it show an error message for each empty value. 我尝试使用explode()函数,但它为每个空值显示一条错误消息。

My model name is "test_model" 我的模型名称是“ test_model”
This is my model function 这是我的模型功能

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;
}

My controller name is "test" 我的控制器名称是“测试”
This is my controller function 这是我的控制器功能

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);
    }
}

My view name is "test_view" 我的视图名称是“ test_view”

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

The result is 结果是

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] => 

)

My controller after modification 修改后我的控制器

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);
    }
}

Thanks for all, Now it is good 谢谢大家,现在很好

只需从checkinout表获取字段time ,使用explode()函数并在check_times上对插入进行check_times

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个表中获取值并插入到第二个表中,同时如何将数据作为后端函数插入第二个表中。 使用laravel 5.2 - how to fetch value from one table and insert into second table while inserting data to the second table as backend function. using laravel 5.2 如果第一个表中不存在,我想从第二个表中获取数据 - I want fetch data from second table if not present in first table 如何从第二个表中获取与第三个表共同的数据列,而第三个表在第一个表上有共同点? - How to get data column from second table that have common to the third table which third table have common on the first table? 如何基于第一张表匹配获取第二张和第三张表的数据 - How to get data of second and third table based of first table match 如何将数据从一个表插入到另一个表 - How to insert data from a table to another table 如何从第二张表查询第一张表的每个结果? - How to query a second table for each result from a first table? 插入到第一个时需要更新第二个表 - Need to update a second table upon insert to first 如何从两个表中选择数据,而两个表只为第一表的一行选择了第二表的第一行 - How to SELECT data from two tables which only first row of second table is selected for one row of first table PHP构建层次结构集,然后从第二个表中插入数据 - PHP Build a hierarchy set then insert data from a second table 将表链接到第二个表并将数据从第一个表多次拉到第二个表 - Linking a table to a second table and pulling data from the first one to the second one multiple times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM