1

Codeigniter 当我提交多个 form_multiselect() 选项时,只有最后一个保存在数据库中。

在我看来 :

<label>Trimestres :</label>
    <div class="controls" >

      <?php $options = array(
            'trim1'  => ' Premier trimestre (Janv,Fév,Mars)',
            'trim2'    => ' Deuxiéme trimestre (Avril,Mai,Juin)',
            'trim3'   => ' Troisiéme trimestre (Juill,Aout,Sept)',
            'trim4' => ' Quatriéme trimestre (Oct,Nov,Déc)',
             );
     echo form_multiselect('trimestres', $options , $this->input->post('trimestres') ? $this->input->post('trimestres') : $participant_sport->trimestres, 'id="trim"'); ?>

     </div>
</div>

在我的控制器中:

public function inscriresport ($id = NULL)
{

// Fetch a participant or set a new one
if ($id) {
$this->data['participant_sport'] = $this->participantsport_m->get($id);
count($this->data['participant_sport']) || $this->data['errors'][] = 'participant non trouvé';
}
else {
$this->data['participant_sport'] = $this->participantsport_m->get_new();
    }


    // Process the form

$this->participantsport_m->array_from_post(array('matricule', 'nom', 'prenom', 'beneficiaire', 'sexe', 'telephone', 'date_naissance', 'date_inscription_sport', 'trimestres' ,'sport_montant_paye', 'sport_debut_periode', 'sport_fin_periode'));
$this->participantsport_m->save($data, $id);
redirect('admin/agent/profile/3608');
    }

    // Load the view
    $this->data['subview'] = 'admin/agent/inscriresport';
    $this->load->view('admin/_layout_main', $this->data);
}

函数 array_from_post() 在 application\core\MY_Model.php 上定义:

public function array_from_post($fields){
    $data = array();
    foreach ($fields as $field) {
        $data[$field] = $this->input->post($field);
    }
    return $data;
}

在我的模型中:

public function get_new()

{

$participant_sport = new stdClass();

$participant_sport->matricule = '';
$participant_sport->nom = '';
$participant_sport->prenom = '';
$participant_sport->beneficiaire = '';
$participant_sport->sexe = '';
$participant_sport->telephone = '';
$participant_sport->date_naissance = '';
$participant_sport->date_inscription_sport = '';
$participant_sport->trimestres = '';
$participant_sport->sport_montant_paye = '';
$participant_sport->sport_debut_periode = '';
$participant_sport->sport_fin_periode = '';
  return $participant_sport;

}

有什么帮助吗?我认为那一定是一个数组,但我不知道该怎么做?

我认为我必须做这样的事情:

foreach($_POST["strategylist[]"] as $s) {
    # do the insert here, but use $s instead of $_POST["strategylist[]"]
    $result=mysql_query("INSERT INTO sslink (study_id, strategyname) " .
       "VALUES ('$id','" . join(",",$s) . "')")
        or die("Insert Error: ".mysql_error());
}

在一行中插入多个选项,但我不知道如何在 codeigniter 中执行此操作

get() 函数:

public function get($id = NULL, $single = FALSE){

    if ($id != NULL) {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->where($this->_primary_key, $id);
        $method = 'row';
    }
    elseif($single == TRUE) {
        $method = 'row';
    }
    else {
        $method = 'result';
    }

    if (!count($this->db->ar_orderby)) {
        $this->db->order_by($this->_order_by);
    }
    return $this->db->get($this->_table_name)->$method();
}
4

2 回答 2

4
于 2013-07-07T21:41:50.687 回答
0

我今天发现有一个简单的方法可以解决这个问题。您必须在 array_form_post 之后序列化 $_POST['trimestres'] 数组。该数组将作为序列化字符串保存到数据库。

public function inscriresport ($id = NULL)
{

// Fetch a participant or set a new one
if ($id) {
$this->data['participant_sport'] = $this->participantsport_m->get($id);
count($this->data['participant_sport']) || $this->data['errors'][] = 'participant non trouvé';
}
else {
$this->data['participant_sport'] = $this->participantsport_m->get_new();
    }


    // Process the form    
$this->participantsport_m->array_from_post(array('matricule', 'nom', 'prenom', 'beneficiaire', 'sexe', 'telephone', 'date_naissance', 'date_inscription_sport', 'trimestres' ,'sport_montant_paye', 'sport_debut_periode', 'sport_fin_periode')); 
$data['trimestres'] = serialize($_POST['trimestres']);

$this->participantsport_m->save($data, $id);
redirect('admin/agent/profile/3608');
    }

    // Load the view
    $this->data['subview'] = 'admin/agent/inscriresport';
    $this->load->view('admin/_layout_main', $this->data);
}

当您只需要这些数据返回表单数据库时,只需使用 php unserialize() 函数。希望这将有助于轻松做到这一点....

-谢谢

于 2015-07-23T13:18:41.780 回答