Search code examples
phpcodeigniterundefinednotice

undefined variable in controller codeigniter but variable still showing the value


i'm newbie in php and codeigniter. i have a problem with undefined variable on controller, but if u see in my web, the value of that variable still showing, it's work correctly. here is my controller :

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

Thanks for your help :)


Solution

  • Try this: You should declare $data2 for an empty string to hold the $data2 concatenated strings.

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