2013-07-02 2 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); 
} 
관련 문제