2013-07-02 114 views
-1

我有一個動態的形式,其中數據被添加到數據庫。 我很困惑,哪種方式更好地從循環 以外的會話中獲取一些數據,或者直接從循環中的會話中獲取數據。 這是我的代碼 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

很明顯,如果避免循環中的會話檢索過程,它會更快。但並不重要。 – Aurel

回答

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