-2

I'm new in Yii programming and i found this notice when i try to make a quiz modul. !

Undefined offset: 4

it indicates error in line 
**if ($data->idjawab == $jawabanku[$x]) **

here's my _form.php code

<form method="post">

    <?php
        $x = 0;
        $static = array('null'  => 'Tidak Tahu'); //Opsi untuk jawaban kosong
        foreach ($soal as $data)
        {
            echo $data->soal."<br>";

            $list = CHtml::listData($data->jawabanjawabans, 'idjawab',  'jawaban');
            echo CHtml::radioButtonList("jawabanku[$x]", '', $list + $static);
            $x++;
            
            echo "<br><br><hr>";
        }
    ?>
    
    <div class="form-actions">
      <div class="col-lg-3">
      <br><br><br><br>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <button type="submit" class="btn btn-primary btn-lg" aria-label="Center">
        <span class="glyphicon glyphicon-save" aria-hidden="true"></span>
          Simpan
        </button>
        <br>
      </div>
    </div>

</form>

here's my crate code

<?php
$this->breadcrumbs=array(
 'Simulasis'=>array('index'),
 'Create',
);
?>
<br>

<div >
            <ol class="breadcrumb"> 
                <li><a href="<?php echo Yii::app()->request->baseUrl; ?>/site/index">Beranda</a></li>
                <li><class="active">Simulasi</a></li>
                
            </ol>

        </div>

<div class="alert alert-dismissable alert-info">
          <button type="button" class="close" data-dismiss="alert">×</button>
          <strong>Penting!</strong> Isikan masing-masing kolom jawaban dengan benar. <br>
          Jawaban benar akan mendapat poin +4. <br>
          Jawaban salah akan mendapatka poin -1. <br>
          Jawaban kosong akan mendapatkan poin 0. <br>
          Kerjakan dengan baik dan benar. Telitilah dalam membaca soal. Kerjakan <b>hanya soal yang yakin dapat dikerjakan</b>
  </div>

<div class="panel panel-primary">
  <div class="panel-heading">
    <h1 class="panel-title" >Simulasi</h1>
  </div>  
  <div class="panel-body">

 <?php $this->renderPartial('_form', array('soal'=>$soal)); ?>

 </div>
</div>

and here's my controller

 public function actionCreate()
 {
  $this->layout='//layouts/column_banksoal';
  if (isset($_POST['jawabanku'])) {
   $jawabanku = $_POST['jawabanku'];
   CVarDumper::dump($jawabanku,15,true);
   $jwbBenar = Soalsoal::model()->findAll(array(
    "select"=>"idjawab",  
    "order"=>"idsoalsoal ASC"));
   $benar = 0; $salah = 0; $kosong = 0; $totalskor=0; $jmlbenar=0; $jmlsalah=0;
   $x = 0;
   
   foreach ($jwbBenar as $data) { 
     if ($data->idjawab == $jawabanku[$x]) {
      $benar++;
     } else if ($jawabanku[$x] == 'null') {
      $kosong++;
     } else {
      $salah++;
     }
     $x++;
    
   }
   
   $jmlbenar = (4*$benar);
   $jmlsalah = ((-1)*$salah);
   $totalskor = $jmlbenar + $jmlsalah;
   //var_dump($totalskor,$jmlbenar,$jmlsalah);
   
   $this->render('hasil', array('benar'=>$benar, 'kosong'=>$kosong, 'salah'=>$salah, 'totalskor'=>$totalskor, 'jmlbenar'=>$jmlbenar, 'jmlsalah'=>$jmlsalah));
  } else {
   $idbidang=Yii::app()->user->idbidang;
   $soal = Soalsoal::model()->findAll(array(
    "select"=>"idsoalsoal,soal,idbidang,gambar,kunci,idjawab", 
    "condition"=>"idbidang=$idbidang", 
    "order"=>"rand() LIMIT 40"));
   
   $this->render("create", array("soal"=>$soal));

  }

 }

i'm new in programming and i new in using yii framework too. If any of you know how to solve this please do. It is for my school task

1 Answers1

3

In PHP you get an "undefined offset" error when you try to read an array element with a key which doesn't exist.

For example:

$arr1 = array('a','b','c');
echo $arr1[50]; // gives undefined offset error

This can be solved by using isset() first. For example:

if(isset($arr1[50]))
{
    echo $arr1[50];
}

So you could probably change your line to this:

if (isset($jawabanku[$x]) && $data->idjawab == $jawabanku[$x])
Mex
  • 1,011
  • 7
  • 16