Search code examples
yii2yii2-basic-app

Yii2 no result message without using gridview


My controller:

 public function actionResults()
{

     $query = User::find();

     $admins = $query->orderBy('id')->andWhere('role_id = 1')->all();

     $searchModel = new AdminSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('results', [
        'admins' => $admins,
        'dataProvider' => $dataProvider,
        'searchModel' => $searchModel,
    ]);
}

Result view:

<table class="table">
<thead>
<tr>
    <th>ID</th>
    <th>Name</th>
    <th>Create Date</th>
</thead>
<tbody>
<?php foreach  ($dataProvider->models as $model){ ?>
<tr>
    <td><?= $model->username; ?></td>

    <td><?=$model->fullname; ?></td>

    <td><?=$model->email; ?></td>
    </tr>

I don't want to use Gridview to so I render the result out like that, but don't know how to throw a no result message.

Please help me.

Thanks


Solution

  • Use php isset() and empty() funtions to check is there is data in the array

      if(isset($dataProvider->models) && !empty($dataProvider->models)){
            //do you foreach loop here
        }else {
            echo 'empty';
        }