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


Codeigniter when i submit more than one option of form_multiselect(), Only just the last one that saved on database

Codeigner当我提交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());
}

在一行中插入多个选择的选项,但我不知道如何在代码点火器中做到这一点

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

如果选择名称(在HTML标记中)是trimestres,它将始终记住上次选择。使用trimestres[]作为名称获取具有所有选定值的数组`

<select name="trimestres[]" multiple …

顺便说一句:我不知道array_from_post()是如何工作的,但它必须将trimestres[]值更改为一个字符串才能将所有值保存在一列中。如果所有值都在一个字符串中,则很难搜索/添加/删除一个值。它是"SQL反模式"。您可以在数据库中为trimestres创建另一个表——一行中有一个值。

编辑:

它会将所有数组更改为字符串,其中元素由,连接。未经测试。

public function array_from_post($fields){
    $data = array();
    foreach ($fields as $field) {
        // print_r($this->input->post($field));
        if( is_array( $this->input->post($field) ) ) {
            $data[$field] = join(",", $this->input->post($field));
        } else {
            $data[$field] = $this->input->post($field);
        }
        // print_r($data[$field]);
    }
    return $data;
}

编辑:

未经测试。

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é';
        // explode to array
        // print_r($this->data['participant_sport']->trimestres); // test before explode
        // $this->data['participant_sport']['trimestres'] = explode(",", $this->data['participant_sport']['trimestres']);
        $this->data['participant_sport']->trimestres = explode(",", $this->data['participant_sport']->trimestres);
        // print_r($this->data['participant_sport']->trimestres); // test after explode
    } else {
        $this->data['participant_sport'] = $this->participantsport_m->get_new();
    }

    // rest of code 
}

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

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

当您只需要从数据库返回此数据时,只需使用phpunserialize()函数。希望这将有助于轻松做到这一点。。。。

-感谢