0

I want to make dropdownlist from single record, I have two table like this:

the tables

Column prodi_pilihan_1_id and prodi_pilihan_2_id referenced from prodi_penerima table.

I want make dropdown for column prodi_pilihan_1_id and prodi_pilihan_2_id but the option text is from prodi_penerima that is nama_prodi_penerima so the dropdown be like this:

<select>
    <option value= '2'> BBB  </option>
    <option value= '4'> DDD  </option>
</select>

So suggest from How to make dropdown in Yii2 from one record, I created controller like this:

 $model4 = Pendaftar::find()
  ->select(['pro1.id_prodi_penerima as id_prodi_pilihan_1','pro1.nama_prodi_penerima as prodi_pilihan_1','pro2.id_prodi_penerima as id_prodi_pilihan_2','pro2.nama_prodi_penerima as prodi_pilihan_2'])
  ->from('pendaftar') 
  ->leftJoin('prodi_penerima as pro1', 'pro1.id_prodi_penerima=pendaftar.prodi_pilihan_1')
  ->leftJoin('prodi_penerima as pro2', 'pro2.id_prodi_penerima=pendaftar.prodi_pilihan_2')
  ->andWhere(['=','id_user_pendaftar',$id])
  ->asArray()
  ->One();
   $rows=\yii\helpers\ArrayHelper::map(array_filter([$model4['prodi_pilihan_1'],$model4['prodi_pilihan_2']]),function($item){
        return $item;
    },function($item){
        return $item;
    });
 //return $this->render('lulus_ujian', ['rows' => $rows]);
    return $this->renderAjax('lulus_ujian', [
        'model' => $this->findModel($id),
        'rows' => $rows,
    ]);

I render that into the view and then into the form, the form is like this:

<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'status_lulus_tes_pendaftar')->dropDownList([ 'yes' => 'Yes', 'no' => 'No', ], ['prompt' => '']) ?>
<?= $form->field($model, 'prodi_lulus')->dropDownList($rows, ['prompt' => '']) ?>
    <?= $form->field($model, 'ket_lulus_ujian')->textarea(['rows' => '6']) ?>
<div class="form-group">
    <?= Html::submitButton('Save', ['class' => 'btn btn-danger']) ?>
</div>

<?php ActiveForm::end(); ?>  

The dropdown is working, but the value and the option is having the same value:

<select>
    <option value= 'BBB'> BBB  </option>
    <option value= 'DDD'> DDD  </option>
</select>

I want save the model where the option value is id_prodi_penerima. What I must do so the option value is 2 and 4 like this:

<select>
    <option value= '2'> BBB  </option>
    <option value= '4'> DDD  </option>
</select>
2
  • 1
    can you add the print_r($model4) , what does it show, and the question you are referring to previously it was for one row, do you still want to draw the menu the same way , means you want to show all the columns in the dropdown, for a single row, and use the option value as another column rather than keeping the same columns for the options value and text. Commented Jun 10, 2018 at 12:05
  • @MuhammadOmerAslam, the result from print_r($model4), Array ( [id_prodi_pilihan_1] => 2 [prodi_pilihan_1] => BBB [id_prodi_pilihan_2] => 4 [prodi_pilihan_2] => DDD ) Commented Jun 11, 2018 at 0:34

2 Answers 2

1

In your case using ArrayHelper does not make any sense, since you need to process only one record and you know exact structure of required array. So just build it directly from query results:

$model4 = Pendaftar::find()
    ->select([
        'pro1.id_prodi_penerima as id_prodi_pilihan_1', 
        'pro1.nama_prodi_penerima as prodi_pilihan_1', 
        'pro2.id_prodi_penerima as id_prodi_pilihan_2', 
        'pro2.nama_prodi_penerima as prodi_pilihan_2'
    ])
    ->from('pendaftar')
    ->leftJoin('prodi_penerima as pro1', 'pro1.id_prodi_penerima=pendaftar.prodi_pilihan_1')
    ->leftJoin('prodi_penerima as pro2', 'pro2.id_prodi_penerima=pendaftar.prodi_pilihan_2')
    ->andWhere(['=', 'id_user_pendaftar', $id])
    ->asArray()
    ->one();
$rows = [
    $model4['id_prodi_pilihan_1'] => $model4['prodi_pilihan_1'], 
    $model4['id_prodi_pilihan_2'] => $model4['prodi_pilihan_2'],
];
return $this->renderAjax('lulus_ujian', [
    'model' => $this->findModel($id),
    'rows' => $rows,
]);
Sign up to request clarification or add additional context in comments.

Comments

1

I think first you have to check what you receive with the $rows variable. Do a variable dump and check whether your $rows variable contains data as follows (Assuming that you expect only 2 pairs).

array(2) { ["2"]=> string(3) "BBB" ["4"]=> string(3) "DDD" }

Else, you have to pass a new array with the above format to render expected results in your dropdown list.

2 Comments

Thanks for your response, the var_dump($rows) give result like this array(2) { ["BBB"]=> string(3) "BBB" ["DDD"]=> string(3) "DDD" }
It means your query is not returning what you expect @eniac05. Please double check your query especially the joins and where condition. It seems like you have over complicated your query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.