How To Get Array Data In View From Controller's Action?

Hello and good morning every one.

I’m facing a problem in passing data of a model from a controller action after finding it using condition.

My controller action is like this.

[b]public function actionView($id) {

    [indent]if (Yii::app()->user->checkRole() == "Client") {


       [indent]$models = BloodCounts::model()->findAll(array('condition' => 'user_iduser=:userId',


            'params' => array(':userId' => intval($_GET['id']))));


        $this->render('view', array(


            'model'=>$models,


        ));[/indent]


    } else {


        [indent]$this->render('view', array(


            'model' => $this->loadModel($id),


            


        ));[/indent]


    }[/indent]

}

[/b]

and in view:-

[b]<table id="bloodViewTable">

    [indent]&lt;tr&gt;


        [indent]&lt;td class=&quot;firstTD&quot;&gt;Date&lt;/td&gt;


        &lt;td class=&quot;middleTD&quot;&gt;Y/M/D&lt;/td&gt;


        &lt;?php


        &#036;count = BloodCounts::model()-&gt;count(array('condition' =&gt; 'user_iduser=:userId',


            'params' =&gt; array(':userId' =&gt; intval(&#036;_GET['id']))));


        for (&#036;i = 0; &#036;i &lt; &#036;count; &#036;i++) {


            ?&gt;


            &lt;td class=&quot;dataTD&quot;&gt;&lt;?php echo &#036;model-&gt;date ?&gt;&lt;/td&gt; [size=&quot;5&quot;][i][b]// here this echo dose not gets the data?[/b][/i][/size]


        &lt;?php } ?&gt;[/indent]


    &lt;/tr&gt;


    &lt;tr&gt;


        [indent]&lt;td class=&quot;firstTD&quot;&gt;WBC&lt;/td&gt;


        &lt;td class=&quot;middleTD&quot;&gt;/mm&lt;/td&gt;


        &lt;?php for (&#036;i = 0; &#036;i &lt; &#036;count; &#036;i++) { ?&gt;


            &lt;td class=&quot;dataTD&quot;&gt;&lt;?php echo &#036;model-&gt;WBC ?&gt;&lt;/td&gt;


        &lt;?php } ?&gt;[/indent]


    &lt;/tr&gt;[/indent]

</table>[/b]

A client has multiple records in bloodcount of different date. In view i don’t find the data of the client.

count gives the correct count for loop. When I print like print_r($models) in action then it prints the whole array with data but when i try to fetch it in view i the all columns get printed without data. i’ve attached files of my viev as well. where the client say john has 3 records in bloodcounts.

$model->date in Your view is an array so I dont see why You want to display array in for loop ?

You need to get 1 record from this array, isnt it ?

And I dont know why You do count inside view ? It should be in controller.




public function actionView($id) {

if (Yii::app()->user->checkRole() == "Client") {

$models = BloodCounts::model()->findAll(array('condition' => 'user_iduser=:userId',

'params' => array(':userId' => intval($_GET['id']))));

$this->render('view', array(

'model'=>$models,

));


} else {

$this->render('view', array(

'model' => $this->loadModel($id),


));


}


}



findAll gives a 2D array

$this->loadModel($id) gives a 1D array

just var_dump or print_r the $model in the view , you will get better idea

Thank you all for your quick reply. I solved the problem but i think it contradicts MVC. I found that my view was unable to get model data from controller so i copied that findAll method in my view.

what i excatly did was in view.

<table id="bloodViewTable">

    &lt;tr&gt;


        &lt;td class=&quot;firstTD&quot;&gt;Date&lt;/td&gt;


        &lt;td class=&quot;middleTD&quot;&gt;Y/M/D&lt;/td&gt;


        &lt;?php


        &#036;count = BloodCounts::model()-&gt;count(array('condition' =&gt; 'user_iduser=:userId',


            'params' =&gt; array(':userId' =&gt; intval(&#036;_GET['id']))));


        &#036;model = BloodCounts::model()-&gt;findAll(array(


            'condition' =&gt; 'user_iduser=:userId',


            'params' =&gt; array(':userId' =&gt; intval(&#036;_GET['id'])),


        ));


        for (&#036;i = 0; &#036;i &lt; &#036;count; &#036;i++) {


            ?&gt;


            &lt;td class=&quot;dataTD&quot;&gt;&lt;?php echo &#036;model[&#036;i]-&gt;date; ?&gt;&lt;/td&gt;


        &lt;?php } ?&gt;


    &lt;/tr&gt;


    &lt;tr&gt;


        &lt;td class=&quot;firstTD&quot;&gt;WBC&lt;/td&gt;


        &lt;td class=&quot;middleTD&quot;&gt;/mm&lt;/td&gt;


        &lt;?php for (&#036;i = 0; &#036;i &lt; &#036;count; &#036;i++) { ?&gt;


            &lt;td class=&quot;dataTD&quot;&gt;&lt;?php echo &#036;model[&#036;i]-&gt;WBC; ?&gt;&lt;/td&gt;


        &lt;?php } ?&gt;


    &lt;/tr&gt;

and i got the desired result.




$this->render('view', array(

'model' => $this->loadModel($id),


));



if you used this

you will get as $model