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

1 回答 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();

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);
}
于 2013-07-02T06:59:33.027 回答