1

i have an applicataion that must to read data from database. i think it simple but why i still got error like this :

A PHP Error was encountered Severity: Notice Message: Undefined variable: data Filename: views/detail_usulan.php

i have tried to repair this code but its still cant work. i'll show u my code. i need your help:(

My controller :

function detail_usulan(){
        $data = array('model_usulan' => $this->model_usulan->get_all());
        //$this->load->view('usulan/detail_usulan', $data);
        $this->render('usulan/detail_usulan', $data);
}

My models :

class model_usulan extends MY_Model{
    function get_all(){
        return $this->db->get('usulan_rkau');
    }
}

My view :

<?php foreach ($data as $view_data) { ?>
            <tr>
            <th> Tahun : </th>
            <td><?php echo $view_data->tahun; ?></td>
            </tr>
dazzle
  • 109
  • 2
  • 15
  • Did you try any of the answers? –  Sep 25 '16 at 11:45
  • Does the function `$this->model_usulan->get_all` return an array which has data as a key? If not, your issue is going to be with the load view part, it would need to be `$this->load->view('usulan/detail_usulan', ['data' => $data]);`, as that's what sets up the variable naming in the views. – gabe3886 Sep 26 '16 at 12:26

2 Answers2

0

You could just try it like below on controller instead of it begin in an array()

$data['model_usulan'] = $this->model_usulan->get_all();

$this->load->view('usulan/detail_usulan', $data);

On your view change this $data

<?php foreach ($data as $view_data) { ?>
   <tr>
      <th> Tahun : </th>
      <td><?php echo $view_data->tahun; ?></td>
   </tr>
<?php }?>

To $model_usulan in the array

<?php foreach ($model_usulan as $view_data) { ?>
   <tr>
      <th> Tahun : </th>
      <td><?php echo $view_data->tahun; ?></td>
   </tr>
<?php }?>

Note: Your class name and file name should have the FIRST letter upper case ONLY same applies for controllers and libraries etc

As explained here Classnames And Filenames

Filename: Model_usulan.php

<?php

class Model_usulan extends CI_Model {

}

Also On your model function try

return $this->db->get('usulan_rkau')->result();

Or

$query = $this->db->get('usulan_rkau');

return $query->result();

And may be change this

MY_Model {

To

CI_Model {
  • thankyou for your help, but its still cant work. they said that the variabel undefined :( @wolfgang1983 – dazzle Sep 25 '16 at 11:50
  • undefined what variable? –  Sep 25 '16 at 11:51
  • On your view did you change it to $model_usulan like what said –  Sep 25 '16 at 11:52
  • variable undefined model_usulan, yes i have changed it. but still cant work @wolfgang1983 – dazzle Sep 25 '16 at 11:55
  • It could be something to do with your render –  Sep 25 '16 at 11:56
  • or just try like `$data['model_usulan'] = $this->model_usulan->get_all();` instead of beign in the array –  Sep 25 '16 at 11:57
  • Did you also do i like in model because you did not use a result or a result_array() other wise it must be with your render. Not much else can help you with –  Sep 25 '16 at 21:10
0

you need to do this change in your controller

function detail_usulan(){
    $data['data'] = array('model_usulan' => $this->model_usulan->get_all());
    $this->load->view('usulan/detail_usulan', $data);
    //$this->render('usulan/detail_usulan', $data);
}

or

change in your view

 foreach ($model_usulan as $view_data) {

Hope this will work.

Dilip Patel
  • 764
  • 15
  • 24
  • thankyou for your help, but its still cant work. they said that the variabel undefined :( @DilipPatel – dazzle Sep 25 '16 at 11:51
  • i've tried both of them but still error @Dilip Patel – dazzle Sep 26 '16 at 06:14
  • if you are using render method the you need to use $this->template->write_view('data', $data); $this->template->render(); and if you are using load->view method then you can direct pass $data $this->load->view('usulan/detail_usulan', $data); – Dilip Patel Sep 26 '16 at 06:20