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

197 views Asked by At

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

3

There are 3 answers

0
Amirfian Alfian On BEST ANSWER

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";
                            
                        }
                        ?>
1
yai On

Try to check whether the data with key 0 is available or not in the database. If there is no data, the issue comes from this line where you defined $dataes with model[0] when that array with key 0 is empty.

<?php $dataes = $dataProvider->models[0]->nim; ?>

Try initializing a variable $a=0 first while not definig the array key, $dataes = $dataProvider->models->nim , then later on call the array with the variable in your if loop.

1
schmauch On

I assume you are using an ActiveDataProvider. If so, you could use if ($dataProvider->count == 0) to check if no data was found.