未定义的偏移量:4

时间:2015-02-15 23:52:16

标签: php arrays yii offset

我是Yii编程的新手,当我尝试制作测验模块时,我发现了这个通知。 !

Undefined offset: 4

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

这是我的_form.php代码

<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>

这是我的箱子代码

<?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>

这是我的控制器

	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));

		}

	}

我是编程新手,也是使用yii框架的新手。如果你们中的任何人知道如何解决这个问题,请做。这是我的学校任务

1 个答案:

答案 0 :(得分:2)

在PHP中,你得到一个&#34;未定义的偏移&#34;尝试使用不存在的键读取数组元素时出错。

例如:

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

首先使用isset()可以解决这个问题。例如:

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

所以你可能会改变你的路线:

if (isset($jawabanku[$x]) && $data->idjawab == $jawabanku[$x])