循环时未定义的偏移量

时间:2015-07-08 07:41:58

标签: php arrays loops undefined offset

我的代码中有一些问题,当我试图给数组变量位置赋值时。代码正在运行,但有很多通知说“未定义的偏移量”这是循环,当我尝试循环我的变量。

                  $query_jur="select * from t_jurusan order by id_jurusan";
                  $sql_jur=mysql_query($query_jur);
                  if (mysql_num_rows($sql_jur)==0) {
                  echo "kosong";
                  $jml_jur=mysql_num_rows($sql_jur);
                  } else {
                  $jml_jur=mysql_num_rows($sql_jur);
                  echo "jumlah jurusan yang diinputkan pada tabel jurusan adalah : ",$jml_jur;
                  echo "<br>";
                  }
                  $query_ins="select * from t_ins_akreditasi_jur order by id_kriteria";
                  $sql_ins=mysql_query($query_ins);
                  if (mysql_num_rows($sql_ins)==0) {
                  $jml_ins=mysql_num_rows($sql_ins);
                  echo "kosong";
                  } else {
                  $jml_ins=mysql_num_rows($sql_ins);
                  echo "jumlah Instrumen yang diinputkan pada tabel Instrumen adalah : ",$jml_ins;
                  }

                  $query_select_hasil="select t_hasil_ins.id_jurusan, t_hasil_ins.id_kriteria, t_ins_akreditasi_jur.id_sub, t_hasil_ins.nilai, t_hasil_ins.hasil, t_hasil_ins.ket from t_hasil_ins, t_ins_akreditasi_jur where t_ins_akreditasi_jur.id_kriteria=t_hasil_ins.id_kriteria order by id_kriteria";
                  $sql_hasil=mysql_query($query_select_hasil);
                  $data_hasil=mysql_fetch_array($sql_hasil);
                  echo $data_hasil['id_sub'];



                    $query_jurusan="select * from t_jurusan order by id_jurusan";

                    $sql_jurusan=mysql_query($query_jurusan);
                    $i=1;


                    $index_jur=array();
                    $index_sub=array();
                    $index_jur_sub=array();
                    $index_bobot=array();
                    $index_nilai=array();
                    $nilai_sementara=array();
                    $index_nilai_akhir_sub=array();
                    $index_nilai_akhir_jur=array();
                    $sql_del1 = mysql_query("delete from t_hasil_rank_instrumen_standar ") or die(mysql_error());
                    while ($data_jurusan=mysql_fetch_array($sql_jurusan)){
                        $index_jur[$i]=$data_jurusan['id_jurusan'];
                        echo "<br>";
                        echo "id jurusan : ",$index_jur[$i];
                        $query_sub="select * from t_sub_kriteria order by id_sub";
                        $sql_sub=mysql_query($query_sub);
                        $j=1;
                        while ($data_sub=mysql_fetch_array($sql_sub)){
                            $index_sub[$j]=$data_sub['id_sub'];
                            echo "-->";
                            echo "id sub : ",$index_sub[$j];
                            $query_jur_sub="select t_hasil_ins.id_jurusan, t_hasil_ins.id_kriteria, t_ins_akreditasi_jur.id_sub, t_hasil_ins.nilai, t_hasil_ins.hasil, t_hasil_ins.ket from t_hasil_ins, t_ins_akreditasi_jur where t_ins_akreditasi_jur.id_kriteria=t_hasil_ins.id_kriteria and t_hasil_ins.id_jurusan='$index_jur[$i]' and id_sub='$index_sub[$j]' order by id_kriteria";
                            $sql_jur_sub=mysql_query($query_jur_sub);
                            $k=1;   
                            $index_nilai_akhir_sub[$i][$j]=0;
                            while ($data_jur_sub=mysql_fetch_array($sql_jur_sub)){
                                $index_jur_sub[$k]=$data_jur_sub['id_kriteria'];
                                $index_nilai[$k]=$data_jur_sub['nilai'];
                                $keterangan[$k]=$data_jur_sub['ket'];
                                echo "<br>";
                                echo " ( ",$index_jur_sub[$k]," ) ";
                                    $query_bobot="select * from t_ins_akreditasi_jur where id_kriteria='$index_jur_sub[$k]'";
                                    $sql_bobot=mysql_query($query_bobot);
                                    $data_bobot=mysql_fetch_array($sql_bobot);
                                    $index_bobot[$k]=$data_bobot['bobot'];
                                    echo "<br>";
                                    echo " dengan bobot ",$index_bobot[$k];
                                    echo "<br>";
                                    echo " dan nilai ",$index_nilai[$k], "tapi, keteranganya ",$keterangan[$k] ;
                                    echo "<br>";
                                    if ($keterangan[$k]=="0"){
                                    $nilai_sementara[$k]=$index_bobot[$k]*0;
                                    } else {
                                    $nilai_sementara[$k]=$index_bobot[$k]*$index_nilai[$k];
                                    }

                                    echo "hasil * kali : ",$nilai_sementara[$k];
                                    $index_nilai_akhir_sub[$i][$j]=$index_nilai_akhir_sub[$i][$j]+$nilai_sementara[$k];
                                $k++;
                                }
                                echo "<br>";
                                echo "HASIL AKHIR JURUSAN ",$index_jur[$i]," SUB ",$j," : ",$index_nilai_akhir_sub[$i][$j];
                                echo "<br>";
                                if($index_nilai_akhir_jur[$i]==0){
                                $index_nilai_akhir_jur[$i]=$index_nilai_akhir_sub[$i][$j];
                                } else {
                                $index_nilai_akhir_jur[$i]=$index_nilai_akhir_jur[$i]+$index_nilai_akhir_sub[$i][$j]; ERROR HAPPEN HERE :(
                                }


                            $j++;
                            }
                            echo "HASIL AKHIR JURUSAN ",$index_jur[$i]," tanpa sub adalah  : ",$index_nilai_akhir_jur[$i];
                            echo "<br>";
                            $sql_insert1 = mysql_query("insert into t_hasil_rank_instrumen_standar values ('$index_jur[$i]','$index_nilai_akhir_jur[$i]','','')");  
                    $i++;

                    }



            ?>` 

1 个答案:

答案 0 :(得分:0)

当您尝试使用没有值的数组变量时会发生这种情况,请尝试:

if (isset($index_nilai_akhir_jur[$i]) && isset($index_nilai_akhir_sub[$i][$j])){
$index_nilai_akhir_jur[$i]=$index_nilai_akhir_jur[$i]+$index_nilai_akhir_sub[$i][$j];}