Yii Framework Forum: Carray Provider - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Carray Provider How to make array provide without using filering. Rate Topic: -----

#1 User is offline   TNC 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-January 13

Posted 24 January 2013 - 11:35 PM

I have an array,
Array
(
[43] => Array
(
[basicProfile] => Array
(
[id] => 1
[user_id] => 1
[first_name] => TNC
)
)
)


I want to make an array provider so that i fed this to CGridView. So i write as;
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'area-grid',
'dataProvider'=> new CArrayProvider(array($array)),
'columns'=>array(
'user_id',
'first_name',
),

));


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

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 24 January 2013 - 11:44 PM

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.
0

#3 User is offline   TNC 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-January 13

Posted 25 January 2013 - 12:22 AM

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..?
0

#4 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 25 January 2013 - 01:04 AM

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.
0

#5 User is offline   TNC 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-January 13

Posted 25 January 2013 - 02:58 AM

Thanks.. Works well.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users