2017-10-01 14 views
0

私はCodeIgniterを使い始めています。私はform_inputの値を出力できません。ここに私のコード:Codeigniter 3:foreach inside form_input

<?= form_input('gender','','type="text" class="form-control form-input" value="'.foreach($profile as $prof){echo $prof->gender;}.'" disabled id="name" style="cursor:default"');?> 

私の構文も正しいですか?

答えて

3

いいえ、構文が正しくありません。 form_inputへのあなたの議論は不吉なことです。あなたが持っているので、入力フィールドは1つしか作成されません。その入力の「価値」は、

value='malefemalefemalemalemalemalsemalefemale', 

のようなものかもしれません。

実際に、あなたが投稿したコードから、あなたがしたいことを知るのは難しいです。私の推測では、プロファイルごとに、この

<?php 
//create an array with attribute values that don't change 
$attributes = [ 
    'class' => "form-control form-input", 
    'style' => "cursor:default", 
]; 

//create a counter 
$i = 0; 

foreach($profile as $prof) 
{ 
    //inputs need a unique "name" and "id", use the counter for that purpose 
    $attributes['name'] = 'gender'.$i; 
    $attributes['id'] = "name".$i; 
    //add the 'value' of each profile to the array  
    $attributes['value'] = $prof->gender; 
    //send the array to form_input 
    echo form_input($attributes, NULL, 'disabled'); 
    echo "<br>"; //new line 
    $i++; //increase value of counter by one for next loop run 
} 

上記の意志の出力テキスト・フィールド(別の行にそれぞれ)です。

`form_input'に関するドキュメント。

入力の「名前」は「gender0」、「gender1」などとなります。これは動作します。それがそれを行う唯一の方法ではありません。入力配列を使用することもできます。その構文はname='gender[]'です。いずれのアプローチも "名前"に対して機能しますが、一意でなければならない "id"属性に対しては機能しません。