Getting Error To Display Best Selling Product

Hi,

I am newbie to YII and loving to learn it. I have a query but i dont do where am I wrong

getting error to display best top 5 selling product in dsplayproduct.php which is in views/members

I have a member table wherein i have (id,productid,memberid) as column

in the member model i have a function

public function getDisplayProduct()

{


$sql="select productid ,count(*)from fc_member group by productid order by count(*) LIMIT 2";


$command=Yii::app()->db->createCommand($sql);


$displayproduct=$command->execute();


}

member controller

public function actiondsplayproduct()

{


$model=new Member;





$this->render('dsplayproduct',array('model'=>$model));


}

view/member/dsplayproduct

<?php

foreach($displayproduct as $value)

{

echo $value;

}

?>

the error appears stating

Undefined variable: displayproduct

Please can someone help me out to figure out this problem or let me know where am i going wrong and how can I resolve this problem

Hi,

Change the following code

view/member/dsplayproduct




<?php


foreach($model->displayproduct() as $value)

{ 


echo $value;

}


?>



You have to understand the assignment of values from controller to view




// displayproduct is your view file name. 

//The second argument states the value assignment

//If you assign array('model' => $model) then it can be accessed from model as $model. 

//If you assign array('mytext' => $model) then it can be accessed from model as $mytext. 


$this->render('dsplayproduct',array('model'=>$model)); 




Cheers!

Happy coding!

if you got output don’t hesitate to press +

you have many ways to get data from database. but now your problem is passing data to view

you are passing $model to displayproduct.php //correct dsplayproduct to what it should be.

so in your view use yii dump() or print_r($model) to see what exactly you have got from db

attention that you don’t passing any variable called “$displayproduct” to view.