[英]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
[英]how to insert the data from the first table to the second table to be like this example
我正在使用codeigniter和mysql,我有两个表。 第一个表名称“ checkinout”:第二个表名称“ check_times”:
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
我想将数据从第一个表插入到第二个表中,如下所示: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 | | |
================================================== ===========================
我尝试使用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;
}
我的控制器名称是“测试”
这是我的控制器功能
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);
}
}
谢谢大家,现在很好
只需从checkinout
表获取字段time
,使用explode()
函数并在check_times
上对插入进行check_times
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.