我有一个动态表单,其中数据被添加到数据库中。 我很困惑,哪种方式更好地从循环外的会话中获取一些数据 或直接我应该从我的循环内的会话中获取数据。 这是我的代码 1-从会话中获取数据并将其放入两个变量中,然后在循环内调用这些变量。
$user_name = $this->input->post('user_name');
$organization_name = $this->input->post('organization_name');
$location = $this->input->post('location');
$duration_time = $this->input->post('duration_time');
$yearly_expences = $this->input->post('yearly_expences');
$expences_source = $this->input->post('expences_source');
$remark = $this->input->post('remark');
//-----
$u_rec_id = $this->session->userdata('rec_id');
$serial = $this->session->userdata('serial');
//-----
$all_array = array();
if( $user_name ) {
if( is_array( $user_name ) ) {
for( $j = 0 ; $j < count( $user_name ) ; $j++ ) {
$this ->ci_form = array(
'user_name' => $user_name[$j],
'organization_name' => $organization_name[$j],
'location' => $location[$j],
'duration_time' => $duration_time[$j],
'yearly_expences' => $yearly_expences[$j],
'expences_source' => $expences_source[$j],
'remark' => $remark[$j],
'userid' => $this->m_auth->get_user_id(),
'u_rec_id' => $u_rec_id,
'serial' => $serial,
'regdate' => date('Y-m-d H:m:s')
);
array_push( $all_array, $this->ci_form );
}
}
}
或者这种方式更好 直接在循环中从会话中获取数据
$user_name = $this->input->post('user_name');
$organization_name = $this->input->post('organization_name');
$location = $this->input->post('location');
$duration_time = $this->input->post('duration_time');
$yearly_expences = $this->input->post('yearly_expences');
$expences_source = $this->input->post('expences_source');
$remark = $this->input->post('remark');
$all_array = array();
if( $user_name ) {
if( is_array( $user_name ) ) {
for( $j = 0 ; $j < count( $user_name ) ; $j++ ) {
$this ->ci_form = array(
'user_name' => $user_name[$j],
'organization_name' => $organization_name[$j],
'location' => $location[$j],
'duration_time' => $duration_time[$j],
'yearly_expences' => $yearly_expences[$j],
'expences_source' => $expences_source[$j],
'remark' => $remark[$j],
'userid' => $this->m_auth->get_user_id(),
'u_rec_id' => $this->session->userdata('rec_id'),
'serial' => $this->session->userdata('serial'),
'regdate' => date('Y-m-d H:m:s')
);
array_push( $all_array, $this->ci_form );
}
}
}
答案 0 :(得分:1)
使用第一种方法并保存一些函数调用:
$user_name = $this->input->post('user_name');
$organization_name = $this->input->post('organization_name');
$location = $this->input->post('location');
$duration_time = $this->input->post('duration_time');
$yearly_expences = $this->input->post('yearly_expences');
$expences_source = $this->input->post('expences_source');
$remark = $this->input->post('remark');
$u_rec_id = $this->session->userdata('rec_id');
$serial = $this->session->userdata('serial');
$all_array = array();
foreach ((array)$user_name as $j=>$each) {
$this ->ci_form = array(
'user_name' => $user_name[$j],
'organization_name' => $organization_name[$j],
'location' => $location[$j],
'duration_time' => $duration_time[$j],
'yearly_expences' => $yearly_expences[$j],
'expences_source' => $expences_source[$j],
'remark' => $remark[$j],
'userid' => $this->m_auth->get_user_id(),
'u_rec_id' => $u_rec_id,
'serial' => $serial,
'regdate' => date('Y-m-d H:m:s')
);
array_push($all_array, $this->ci_form);
}