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

时间:2013-07-07 20:46:34

标签: php html codeigniter

当我提交form_multiselect()的多个选项时,

Codeigniter,只是保存在数据库中的最后一个选项。

在我看来:

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

2 个答案:

答案 0 :(得分:4)

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

<select name="trimestres[]" multiple …

顺便说一下: 我不知道array_from_post()是如何工作的,但它必须将trimestres[]值更改为一个字符串,以便将所有值保存在一列中。如果所有值都在一个字符串中,则很难搜索/添加/删除一个值。这是“SQL Antipattern”。您可以在数据库中为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 

}

答案 1 :(得分:0)

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

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()函数。 希望它能帮助你轻松做到这一点......

-Thanks