Arrays as dataprovider

I am pulling some data from google and it is returned as an array.

I want to use it in a grid or list and am trying to use

$this->widget(‘zii.widgets.CListView’, array(

'dataProvider'=>$dataProvider,


'itemView'=>'_view',


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


    ));.

How would I assign the array to the dataProvider?

Any help would be appreciated.

I have created a data provider for an array for one of my projects. It worked in my case , just try it




<?php

class SDataProvider extends CDataProvider {

/**

* Constructor.

* @param string the model class. This will be assigned to the {@link modelClass} property.

* @param array configuration (name=>value) to be applied to this data provider.

* Any public properties of the data provider can be configured via this parameter

*/

 public function __construct($id,$data)	{

  $this->setData($data);

  $this->setId($id);

 }

 //put your code here

  protected function calculateTotalItemCount() {

    return count($this->getData());

  }

  protected function fetchData() {

    return $this->getData();

  }

  protected function fetchKeys() {

    foreach ($this->getData() as $key=>$value) {

      $keys[]= $key;

    }

    return $keys;

  }

}

?>



Then before the widget :




<?php 

$dataProvider = new SDataProvider("grid",$dataArray);


$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

    'columns'=>array(

....

?>



Spyros, thank you for your SDataProvider, truly fantastic!

I did need to modify it to allow for empty result sets. $keys should be declared in case the foreach runs zero times:


protected function fetchKeys() {

  $keys = array();

  foreach ($this->getData() as $key => $value) {

    $keys[] = $key;

  }

  return $keys;

}

Thanks again for posting this. Much appreciated!

Didn’t use it yet, but shouldn’t CArrayDataProvider do the same?

Just wanted to note that the CArrayDataProvider() indeed does the same, just in case anyone had a similar problem

Your solution is work with CListView, but with CGridView not :(why?