1

I want to display the specific data from database using <?= Html::encode() ?>

Let say, i get the specific column of the model as below:

   <?php $model = ExampleModule::find()->select('anycolumn')->all(); ?>

And then, what should i write to the <?= Html::encode(anystatement) ?>to display the values ?

3 Answers 3

1

you can encode the single column result and ->all() return a collection fo models

so first you should access to single model, eg: assuming you obtain your collection of models as an array

    <?php $model = ExampleModule::find()->select('anycolumn')->asArray->()all(); ?>

you can encode the single column result for the first model this way

    <?= Html::encode($model[0]['your_column']); ?>
Sign up to request clarification or add additional context in comments.

Comments

1

Get all data first .

First step

<?php $model = ExampleModule::find()->select('anycolumn')->asArray()->all(); ?>

Second step

<?= Html::encode($model[0]['anycolumn']); ?>

Comments

1

First of all ExampleModule::find()->select('anycolumn')->all() returns an array of records.

If you want to get first found record you need to use

<?php
$model = ExampleModule::find()->select('anycolumn')->one();
?>

Then

<?= Html::encode($model->anycolumn) ?>

Or if you want to display all records:

<?php
foreach (ExampleModule::find()->select('anycolumn')->all() as $model) {
    echo Html::encode($model->anycolumn) . '<br>';
}
?>

Comments

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.