1

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

1行で選択された複数のオプションを挿入するには、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() function を使用してください。これを簡単に行うのに役立つことを願っています....

-ありがとう

于 2015-07-23T13:18:41.780 に答える