Retrieving Data from another model and...

Hello everybody.

I’m trying to do a Likert survey in a table, but so far i haven’t accomplish to do what i’m trying to.

What i want to do is with a for each bring some data from a model and then generate a label with that information(Done, i guess) and for each label generated in the previous for each , i want to generate 5 radiobuttons.

This is my code:




<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;

use yii\db\ActiveRecord;

use app\models\Answers;

use app\models\Questions;





/* @var $this yii\web\View */

/* @var $model app\models\Surveys */

/* @var $form yii\widgets\ActiveForm */

?>


<div class="encuestas-form">


    <?php $form = ActiveForm::begin(); ?>


    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'objective')->textInput(['maxlength' => true]) ?>


 


<?php $data = Surveys::find()->all(); ?>





<?php $answers = Answers::find()->all();?>


<?php 

foreach (Questions::find()->all() as $question) {

    $data = $question->questions;

     echo "<br>";

     echo $label = Html::label(

            $data, 'Question',

            ['class' => 'control-label', 'style' => 'color:black']

     );

     echo "</br>

    foreach ($question as $radio) {

        echo Html::radio('agree', true, ['label' => 'I agree', 'value' => '1']);;

        echo Html::radio('second', true, ['label' => 'Disagree', 'value' => '1']);;

 

          //It generates only 4 and with same name(so the problem i can check all buttons)

    }


   

  

    

}

 ?>










   

  

    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>




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

       


</div>




i really appreciate your help

Hi NetoLozano, when you’re working with radios for an attribute you need to set the same name for both radios for each record obtained in your find() method, also can’t just checked the two of them because they’re i.e. to choose one from many answers that you have for a question etc, and their values should be different as well to identify which answer did you choose.

in this way you can try this:




echo "</br>

    foreach ($question as $radio) {

        echo Html::radio('answer', true, ['label' => 'I agree', 'value' => '0']);;

        echo Html::radio('answer', false, ['label' => 'Disagree', 'value' => '1']);;

                        ("same name", only 1 checked, distinctive labels, whatever value you want to put in(differents)),

    }



Giving the same name to radios will prevent the possibility to check them all.