1

i want to display array in view form but i don't know how ? can any one help me : my controller code is :

$query = new Query;
$query 
    ->select(['Day_Name']) 
    ->from('days')
    ->join( 'INNER JOIN','doctorsworkday','doctorsworkday.Day_id =days.dayid');
$command = $query->createCommand(); 
$dataProvider = $command->queryAll(); 
$dataProvider= array($dataProvider);
return $this->render('view', 
    [
        'model' => $this->findModel($id),
        'days'=>$days,
        'doctorsworkday'=>$doctorsworkday,
        'dataProvider' => $dataProvider,
    ])

my view code is :

<?= 
    DetailView::widget([
        'model' => $dataProvider,
        'attributes' => [
            'Day_Name' 
         ],
     ]) 
?>

but its shows : (not set)

when i use vat_dump($dataProvider) there is an array .. but i don't know how to show it

enter Var_Dump

1 Answer 1

2

Well firstly you are using a DetailView for displaying multiple results. DetailView is used to display the detail of a single data model as said in the docs. You could use a GridView to display multiple results. GridView accepts a dataProvider not an array so you'll need to turn your array into a dataProvider.

fortunately you can use the class ArrayDataProvider to do just that:

$gridViewDataProvider = new \yii\data\ArrayDataProvider([
    'allModels' => $dataProvider,
    'sort' => [
        'attributes' => ['Day_Name'],
    ],
    'pagination' => ['pageSize' => 10]
]);

something like this should work.

Then you pass this $gridViewDataProvider to the gridview like this:

<?= GridView::widget([
    'dataProvider' => $gridViewDataProvider,
    'columns' => [
        'name'
    ]
]) ?>

Also note that in your controller you wrapped your $dataProvider in an array with this line:

$dataProvider= array($dataProvider);

You should remove that line and then everything should work as far as I can tell.

Sign up to request clarification or add additional context in comments.

1 Comment

it works fine .. thank you very much .. i appreciate your help :)

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.