控制器 codeigniter 中的未定义变量,但变量仍显示该值

undefined variable in controller codeigniter but variable still showing the value

我是 php 和 codeigniter 的新手。 我在控制器上遇到未定义变量的问题,但如果你在我的网站上看到该变量的值仍然显示,它工作正常。这是我的控制器:

public function carisoal(){
    $kode_mat = $this->input->post('kode_mat');
    $hasil = $this->dos->getsoal($kode_mat);

    $data2 .= "<thead><tr><th> SOAL</th>    <th > KUNCI JAWABAN </th><th > DETAIL</th></tr> </thead>";
    foreach ($hasil as $sl) {
        $data2 .= "<tbody><tr> <td>".$sl['pertanyaan']."</td> <td>".$sl['kunci']."</td> <td> Detail </td> </tr> </tbody";
    }
    echo $data2;

}

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data2

Filename: controllers/dosen.php

Line Number: 75

感谢您的帮助:)

试试这个:您应该为空字符串声明 $data2 以保存 $data2 串联字符串。

public function carisoal(){
  $kode_mat = $this->input->post('kode_mat');
  $hasil = $this->dos->getsoal($kode_mat);

  $data2 = "";

  $data2 .= "<thead><tr><th> SOAL</th>    <th > KUNCI JAWABAN </th><th > DETAIL</th></tr> </thead>";

   foreach ($hasil as $sl) {
      $data2 .= "<tbody><tr> <td>".$sl['pertanyaan']."</td> <td>".$sl['kunci']."</td> <td> Detail </td> </tr> </tbody";
   }
echo $data2;
}