1

i have used ArrayDataProvider in controller yii2.

<?php

public function actionPagination_product2(){
$tb_tab=Tablet::tableName();
Digital.inner_memory')->asArray()->all();
$data = Digital::find()->joinWith('tablet',true,'Left Join')->where('Tablet.sup_id = Digital.id')->asArray()->all();
$dataProvider = new ArrayDataProvider([
    'allModels' => $data,
	 'sort' => [
        'attributes' => ['id'],
    ],
    'pagination' => [
        'pageSize' => 20,
    ],
]);

   return $this->render('pagination_pro2', [
      'dataProvider' => $dataProvider,
   ]);
   
   ?>

and in view pagination_pro2 i have

<?php
   use yii\widgets\ListView;
   echo ListView::widget([
      'dataProvider' => $dataProvider,
	 
      'itemView' => '_page1',
   ]);
?>

and in view _page1 i have

<?php
   use yii\helpers\Html;
   use yii\helpers\HtmlPurifier;
?>
<div class = "user">


   <?php 
foreach($model as $attribute => $value) {
   // do your stuff here
if(isset($model['sim_num'])){
echo "aaaa";
}
}
?>

</div>

that works true.but i want to access one by one of model attributes.

are that works true? what have to do? tnx

1 Answer 1

1

To access model attributes one by one in view _page1 use foreach loop:

  1. If your data is just array:

    foreach($model as $attribute => $value) {
       // do your stuff here
    }
    
  2. If your array is array of models:

    foreach($model->attributes as $attribute => $value) {
       // do your stuff here
    }
    
Sign up to request clarification or add additional context in comments.

14 Comments

tnx.it has to be work but shows an error as : Trying to get property of non-object in foreach line how to fix it ? thank you
thank you..that error resolved ;)) just is there any way to access each attribute of each element seperately ? for example like:$model['sim_num'] . how can do that ?
If your data is just key=>value array, you can access it normally like array: if(isset($model['sim_num'])) { // do your stuff on $model['sim_num']); }
i have that in array but when use isset not working :(( excuse me to ask a lot :))
It's okay, i like to fix stuff like this :P Can you update question with code how you use it, and if you can, result of var_dump($model);die;?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.