[英]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.