phpyii2yii2-basic-app

Yii2 Error Undefined offset: 0 (Theres no Match in Database)


sorry for my dumb question. This My SiteController.

    public function actionIndex()
    {
        $searchModel = new MahasiswaSearch();
        $dataProvider = $searchModel->search($this->request->queryParams);

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

this my form _search

<div class="control control-expanded">

    <?php $form = ActiveForm::begin([
        'action' => ['index'],
        'method' => 'get',
    ]); ?>

    <?php // echo $form->field($model, 'id') ?>

    <?= $form->field($model, 'nim', [
           'inputOptions' => ['autofocus' => 'autofocus', 'class' => 'input' ,'required'=>true]
     ])->input('nim', ['placeholder' => "Nomor Induk Mahasiswa"])->label(false); ?>

</div>

<div class="control control-expanded">
    <?= Html::submitButton('Cari', ['class' => 'button button-primary button-block']) ?>
</div>

there is my index.php

<?php $dataes = $dataProvider->models[0]->nim;if ($dataes=="") { ?>


                    <?php } else {?>
                    <div class="hero-copy hero-2" style="z-index:30;background-color: rgba(56, 76, 76, 0.5);padding-top:8px; margin-top:8px;">
                        <p class="hero-paragraph is-revealing" style="text-align:center;">PENGUKUHAN MAHASISWA BARU</p>


                        <?php
                             echo "<p style=padding-left:10px;> NIM : " . $dataProvider->models[0]->nim ;echo " </p> \n";
                             echo "<p style=padding-left:10px;> NAMA : " . $dataProvider->models[0]->nama ;echo " </p> \n";
                             echo "<p style=padding-left:10px;> ANGKATAN : " . $dataProvider->models[0]->angkatan ;echo " </p> \n";
                             echo "<p style=padding-left:10px;> PRODI : " . $dataProvider->models[0]->prodi ;echo " </p> \n";
                             echo "<p style=padding-left:10px;> LOKASI KEGIATAN : " . $dataProvider->models[0]->ruangan ;echo " </p> \n";
                            
                        }
                        ?>

my problem, i wanna check $dataes = not found in database. it must be :

<p> error Messsage </p>

but when i find on google theres no problem same like me. or example create search using form yii2, and i try move route when error to error site it's not working. Undefined offset: 0, thanks if you know solve my problem here

enter image description here


Solution

  • need change index.php, im false logical if:

      <?php $dataes = $dataProvider->models ? $dataProvider->models[0]->nim : ''; 
                        if ($dataProvider->count == 0) {
    
        
                         } elseif ($dataes=="") { ?>
                        <p>Wow</p>
                        <?php }else{ ?>
                        <div class="hero-copy hero-2" style="z-index:30;background-color: rgba(56, 76, 76, 0.5);padding-top:8px; margin-top:8px;">
                            <p class="hero-paragraph is-revealing" style="text-align:center;">PENGUKUHAN MAHASISWA BARU</p>
    
                            <?php
                                 echo "<p style=padding-left:10px;> NIM : " . $dataProvider->models[0]->nim ;echo " </p> \n";
                                 echo "<p style=padding-left:10px;> NAMA : " . $dataProvider->models[0]->nama ;echo " </p> \n";
                                 echo "<p style=padding-left:10px;> ANGKATAN : " . $dataProvider->models[0]->angkatan ;echo " </p> \n";
                                 echo "<p style=padding-left:10px;> PRODI : " . $dataProvider->models[0]->prodi ;echo " </p> \n";
                                 echo "<p style=padding-left:10px;> LOKASI KEGIATAN : " . $dataProvider->models[0]->ruangan ;echo " </p> \n";
                                
                            }
                            ?>