Undefined variable: dataProvider

I have this code in my Article Controller


public function actionIndex()

	{

		

		$dataProvider=new CActiveDataProvider('Page', array(

    'criteria'=>array(

        'condition'=>'status=1',

        'condition'=>'category_id=1',

        'order'=>'postDate DESC',

        

    ),

   

    'pagination'=>array(

        'pageSize'=>20,

    ),

));

// $dataProvider->getData() will return a list of Post objects

		

		$this->render('index',$dataProvider);

	}

and in my index.php i have


<?php $this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

	'template'=>"{items}\n{pager}",

)); ?>

I am getting error Undefined variable: dataProvider

Why am i getting this error i think everything i did was just fine !

Change




$this->render('index',$dataProvider);



in




$this->render('index', array('dataProvider' => $dataProvider));



It worked out thanks :)

But i am having blank view with header and navigation only…

What i tried to was getting views of article which falls under category 1.

To detail i have two table in my database

1> category

2> page

category_id is a foreign key in page table and i am trying to pull all articles which have category_id=1 using above code.

But i am getting blank display, no data are fetched

You have multiple condition attribute in criteria property of Data Provider.

Try this:




<?php

$dataProvider = new CActiveDataProvider('Page', 

	array(

		'criteria' => array('condition' => 'status=1 AND category_id=1', 'order' => 'postDate DESC', ), 

		'pagination' => array('pageSize' => 20, ), 

	)

);

?>



No results yet!


$dataProvider = new CActiveDataProvider('Page',          array(                 'criteria' => array('condition' => 'status=1 AND category_id=1', 'order' => 'postDate DESC', ),                  'pagination' => array('pageSize' => 20, ),          ) );

no displays and fetched data in _view.php file

Are you sure that Post model contains data if you filter by status=1 AND category_id=1 ?

I have this stuff in my _view file,is this affecting my display ?


<div class="view">  	

<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b> 	

<?php echo CHtml::link(CHtml::encode($data->id),array('view','id'=>$data->id)); ?> 	<br />

<b><?php echo CHtml::encode($data->getAttributeLabel('summary')); ?>:</b> 	

<?php echo CHtml::encode($data->summary); ?> 	<br /> </div>

Its Page model let me copy my model here, i hope i am not troubling you. sorry !


<?php




class Page extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Page the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'page';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('category_id, topic_id, title, url', 'required'),

			array('category_id, topic_id, hits, status', 'numerical', 'integerOnly'=>true),

			array('title, seoDesc, image, tags', 'length', 'max'=>245),

			array('url', 'length', 'max'=>100),

			array('seoTitle', 'length', 'max'=>120),

			array('summary, detail, postDate', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, category_id, topic_id, title, url, seoTitle, seoDesc, image, summary, detail, tags, hits, postDate, status', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'category' => array(self::BELONGS_TO, 'Category', 'category_id'),

			'topic' => array(self::BELONGS_TO, 'Topic', 'topic_id'),

			'pagefacts' => array(self::HAS_MANY, 'Pagefact', 'page_id'),

			'pageimages' => array(self::HAS_MANY, 'Pageimages', 'page_id'),

			'pagevideoses' => array(self::HAS_MANY, 'Pagevideos', 'page_id'),

			'tags0' => array(self::HAS_MANY, 'Tag', 'page_id'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'category_id' => 'Category',

			'topic_id' => 'Topic',

			'title' => 'Title',

			'url' => 'Url',

			'seoTitle' => 'Seo Title',

			'seoDesc' => 'Seo Desc',

			'image' => 'Image',

			'summary' => 'Summary',

			'detail' => 'Detail',

			'tags' => 'Tags',

			'hits' => 'Hits',

			'postDate' => 'Post Date',

			'status' => 'Status',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id);

		$criteria->compare('category_id',$this->category_id);

		$criteria->compare('topic_id',$this->topic_id);

		$criteria->compare('title',$this->title,true);

		$criteria->compare('url',$this->url,true);

		$criteria->compare('seoTitle',$this->seoTitle,true);

		$criteria->compare('seoDesc',$this->seoDesc,true);

		$criteria->compare('image',$this->image,true);

		$criteria->compare('summary',$this->summary,true);

		$criteria->compare('detail',$this->detail,true);

		$criteria->compare('tags',$this->tags,true);

		$criteria->compare('hits',$this->hits);

		$criteria->compare('postDate',$this->postDate,true);

		$criteria->compare('status',$this->status);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}

After defining $dataProvider,




var_dump($dataProvider->getData());



to see if it contents data.

Yes I get a valid response with arrays full of data. check the attachment

Try to display




var_dump($dataProvider);



in view and not in controller, to see if variable is available in view.

Then in _view file of ListView, try to




var_dump($data);



at top of file, to see if ListView is dispatching all data in dataProvider.

no nothing displaying in both files !

So $dataProvider in view is different from $dataProvider in Controller.

Are you sure that in your view don’t init another $dataProvider?

Post view file.

It i similar to my _view file.


<div class="view">

<?php var_dump($dataProvider); ?>

	<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($dataPrivder->id),array('view','id'=>$data->id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('title')); ?>:</b>

	<?php echo CHtml::encode($data->title); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('url')); ?>:</b>

	<?php echo CHtml::encode($data->url); ?>

	<br />




	<b><?php echo CHtml::encode($data->getAttributeLabel('summary')); ?>:</b>

	<?php echo CHtml::encode($data->summary); ?>

	<br />


	</div>

is it your view so:




<?php $this->widget('zii.widgets.CListView', array(

        'dataProvider'=>$dataProvider,

        'itemView'=>'_view',

        'template'=>"{items}\n{pager}",

)); ?>



I am sorry i did not get you … The above code is in my index.php , my view files i.e [_view.php and view.php] both have





<div class="view">

<?php #var_dump($dataProvider); ?>

	<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->id),array('view','id'=>$data->id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('title')); ?>:</b>

	<?php echo CHtml::encode($data->title); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('url')); ?>:</b>

	<?php echo CHtml::encode($data->url); ?>

	<br />




	<b><?php echo CHtml::encode($data->getAttributeLabel('summary')); ?>:</b>

	<?php echo CHtml::encode($data->summary); ?>

	<br />


	</div>

controller:




public function actionIndex()

        {

             

           $dataProvider=new CActiveDataProvider('Page', array(

               'criteria'=>array(

                    'condition'=>'status=1 AND category_id=1',

                    'order'=>'postDate DESC',

                ),

   

                'pagination'=>array(

                     'pageSize'=>20,

                 ),

            ));

                

            $this->render('index', array('dataProvider' => $dataProvider));

        }



view:




<?php $this->widget('zii.widgets.CListView', array(

        'dataProvider'=>$dataProvider,

        'itemView'=>'_view',

        'template'=>"{items}\n{pager}",

)); ?>



_view:




<div class="view">

<?php #var_dump($dataProvider); ?>

        <b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>

        <?php echo CHtml::link(CHtml::encode($data->id),array('view','id'=>$data->id)); ?>

        <br />


        <b><?php echo CHtml::encode($data->getAttributeLabel('title')); ?>:</b>

        <?php echo CHtml::encode($data->title); ?>

        <br />


        <b><?php echo CHtml::encode($data->getAttributeLabel('url')); ?>:</b>

        <?php echo CHtml::encode($data->url); ?>

        <br />




        <b><?php echo CHtml::encode($data->getAttributeLabel('summary')); ?>:</b>

        <?php echo CHtml::encode($data->summary); ?>

        <br />


        </div>



Is it correct?

Thanks alot for your help Fabrizio Caldarelli i sorted out the problem and now my data are visible.

:)

I’m happy to help you but … what was the problem?

Well, to admit it was a silly mistake of mine, as some unwanted codes from my previous try were left uncommented. I forgot to comment them :lol: