1

I tried my best to solve this , but I can't seem to figure out where the problem is. Here is my code:

views/supermarkets/index.php:

<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Supermarkets</h1>
<ul>
<?php
    $array = (array) $supermarkets;


    $this->widget('zii.widgets.grid.CGridView', array(
        'id' => 'supermarkets-grid',
        'dataProvider' => $model->search(),
        'filter' => $model,
        'columns' => array(
            'name',
            'location',
            'telephone',
            'fax',
            'website'
        ),
    ));



function build_table($array){

    // start table

    $html = '<table class="altrowstable" id="alternatecolor">';

    // header row

    $html .= '<tr>';

    foreach($array[0] as $key=>$value){

            $html .= '<th>' . $key . '</th>';

        }

    $html .= '</tr>';

    // data rows

    foreach( $array as $key=>$value){

        $html .= '<tr>';

        foreach($value as $key2=>$value2){

            $html .= '<td>' . $value2 . '</td>';

        }

        $html .= '</tr>';

    }

    // finish table and return it

    $html .= '</table>';

    return $html;

}



echo build_table($array);

?>


<?= LinkPager::widget(['pagination' => $pagination]) ?>

Supermarkets.php:

<?php

namespace app\models;

use yii\db\ActiveRecord;

class Supermarkets extends ActiveRecord
{

 public function search()
{

    $criteria=new CDbCriteria;

    $criteria->compare('name',$this->Name,true);
    $criteria->compare('location',$this->Location,true);
    $criteria->compare('telephone',$this->Telephone,true);
    $criteria->compare('fax',$this->Fax,true);
    $criteria->compare('website',$this->Website,true);

    return new CActiveDataProvider(get_class($this), array(
        'criteria'=>$criteria,
        'sort'=>array(
            'defaultOrder'=>'name ASC',
        ),
        'pagination'=>array(
            'pageSize'=>20
        ),
    ));
}

SupermarketsController.php:

<?php

namespace app\controllers;

use yii\web\Controller;
use yii\data\Pagination;
use app\models\Supermarkets;

class SupermarketsController extends Controller
{
    public function actionIndex()
    {
        $query = supermarkets::find();

        $pagination = new Pagination([
            'defaultPageSize' => 20,
            'totalCount' => $query->count(),
        ]);

        $supermarkets = $query->orderBy('Name')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

        return $this->render('index', [
            'supermarkets' => $supermarkets,
            'pagination' => $pagination,
        ]);



        $model =new Supermarkets('search');
        if(isset($_GET['Supermarkets']))
            $model->attributes =$_GET['Supermarkets'];

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

    }
    public function actionName(){

    $model = new Supermarkets();



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

This is the error I'm getting:

Undefined variable: model in index.php at ('dataProvider' => $model->search() ) I am trying to add Search and filter criteria based on the following question How to add Search and Filter Criteria in Yii

Could you please help me?

3 Answers 3

1

in your SupermarketsController.php: you have a mistake:

you have rendered this

    return $this->render('index', [
        'supermarkets' => $supermarkets,
        'pagination' => $pagination,
    ]);

which doesn't have any model named variable,

and later you have

    $model =new Supermarkets('search');
    if(isset($_GET['Supermarkets']))
        $model->attributes =$_GET['Supermarkets'];

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

which will be useless, because you are returning before that,

so you can grab that variable and render it along with the first render

Sign up to request clarification or add additional context in comments.

Comments

0

This will work. You have rendered 2 time index view. mergethem into one

 public function actionIndex()
    {
        $query = supermarkets::find();

        $pagination = new Pagination([
            'defaultPageSize' => 20,
            'totalCount' => $query->count(),
        ]);

        $supermarkets = $query->orderBy('Name')
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();



        $model =new Supermarkets('search');
        if(isset($_GET['Supermarkets']))
            $model->attributes =$_GET['Supermarkets'];


        return  $this->render('index', array(
            'model'        => $model,
            'supermarkets' => $supermarkets,
            'pagination'   => $pagination,
        ));

    }

1 Comment

Same answer as previous
0

You have rendered two time in your controller,

$model =new Supermarkets('search');
        if(isset($_GET['Supermarkets']))
            $model->attributes =$_GET['Supermarkets'];


        return  $this->render('index', array(
            'model'        => $model,
            'supermarkets' => $supermarkets,
            'pagination'   => $pagination,
        ));

Please above code by replacing below code

return $this->render('index', [
            'supermarkets' => $supermarkets,
            'pagination' => $pagination,
        ]);



        $model =new Supermarkets('search');
        if(isset($_GET['Supermarkets']))
            $model->attributes =$_GET['Supermarkets'];

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

Comments

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.