Carray Provider

I have an array,

[b]Array

(

[43] => Array


    (


        [basicProfile] => Array


            (


                [id] => 1


                [user_id] => 1


                [first_name] => TNC


           )


  )

)[/b]

I want to make an array provider so that i fed this to CGridView. So i write as;

[b]$this->widget(‘zii.widgets.grid.CGridView’, array(

'id'=>'area-grid',


'dataProvider'=> new CArrayProvider(array($array)),


'columns'=>array(


'user_id',


'first_name',


),

));[/b]

$array is my variable containing the above data. This throws an undefined offset exception. Any help?

Dear Friend

I hope the following is helpful.




<?php $arr=array(

                array("id"=>1,"fruit"=>"apple","color"=>"red"),

                array("id"=>2,"fruit"=>"orange","color"=>"orange"),

                array("id"=>3,"fruit"=>"grape","color"=>"green"),

                array("id"=>4,"fruit"=>"banana","color"=>"yellow"),              

                );

?>

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

        'id'=>'customer-grid',

        'dataProvider'=>new CArrayDataProvider($arr),

        'columns'=>array(

            'id',

            'fruit',

            'color',            

        ),

));

?>



Ensure that each array element is an array maintaining uniformity.

Do not forget to put id in each element.

If your array record do not contain keyField ie) id

You can do something like this.




<?php $arr=array(

                array("fruit"=>"apple","color"=>"red"),

                array("fruit"=>"orange","color"=>"orange"),

                array("fruit"=>"grape","color"=>"green"),

                array("fruit"=>"banana","color"=>"yellow"),              

                );

$dataProvider=new CArrayDataProvider($arr);

$dataProvider->keyField=false;

?>

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

        'id'=>'customer-grid',

        'dataProvider'=>$dataProvider,

        'columns'=>array(

            'fruit',

            'color',            

        ),

));

 ?>




It is advised to put keyField and also as id to avoid confusion.

Regards.

So the answer is simply,

$dataProvider=new CArrayDataProvider($array); ??

This throws undefined offset 1, when $dataProvider -> keyField = TRUE;

As you see, i have 3-Dimensional array, where to define keyfields…?

Dear Friend

The key field is unique identifier for each array element.It should not be included in the inner arrays.

Following is one complete solution involving pagination and sorting in an multidimensional array.




$arr=array(


array('id'=>1,'profile'=>array('name'=>'jack','age'=>10,'sex'=>'male')),

array('id'=>2,'profile'=>array('name'=>'jill','age'=>8,'sex'=>'female')),

array('id'=>3,'profile'=>array('name'=>'jhon','age'=>6,'sex'=>'male')),

array('id'=>4,'profile'=>array('name'=>'jerry','age'=>4,'sex'=>'male')),




);

?>

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

        'id'=>'customer-grid',

        'dataProvider'=>new CArrayDataProvider($arr,array(

        'sort'=>array(

        'attributes'=>array('id','profile.name','profile.age','profile.sex')

        ),

        'pagination'=>array('pageSize'=>2)

        )),

        'columns'=>array(

            'id',

            array(

            'header'=>'Name',

            'name'=>'profile.name',

            'value'=>'$data["profile"]["name"]'

            ),

            array(

            'header'=>'Age',

            'name'=>'profile.age',

            'value'=>'$data["profile"]["age"]'

            ),

            array(

            'header'=>'Sex',

            'name'=>'profile.sex',

            'value'=>'$data["profile"]["sex"]'

            ),

        ),

));

?>




Regards.

Thanks… Works well.