Cignitor
Cignitor

Reputation: 1097

Codeigniter view shows 0 record

i've insert 5 rows into ms_kategori_material table, and made some model here

function read()
{   
    $query = $this->db->get('ms_kategori_material');

    if($query->num_rows()>0)
    {            
        foreach ($query->result_array() as $value) {
            echo $value['Kode_Kategori_Material_Jasa'];
            echo $value['Nama_Material_Jasa'];
        }
        return $value;
    }
    else
    {
        return null;
    }
}

and controllers

function index()
{
      $data['kirim'] = $this->m_kategorimaterial->read();
      echo $data; //returns KKMJ001batuKKMJ002batuKKMJ003batuKKMJ004batuKKMJ005batuArray
      $this->load->view('v/vkategorimaterial',$data);

}

and the views

<?php  
                if ( !empty($kirim) ) {  
                 $no = 1;   
                 foreach ($kirim as $row) { ?>  
                 <tr id="row">  
                  <td id="no"><?php echo $no;?></td>  
                  <td id="judul"><?php echo $row->Kode_Kategori_Material_Jasa;?></td>  
                  <td id="kategori"><?php echo $row->Nama_Material_Jasa;?></td>  
                    </tr>  ?>
                    <?php  
                     $no++;  
                    }  
                   } else { ?>  
                                <tr id="row">  
                                <td colspan="6" align="center">Data tidak ditemukan</td>  
                               //the screen shows 'data tidak ditemukan' </tr>  
                            <?php  
                   }  
                   ?>  

this is so confusing, since i can see the data in the controller, then pass them into the views, views said no data received

Upvotes: 2

Views: 1265

Answers (3)

Manoj
Manoj

Reputation: 373

You are actually not returning the results in your model,

replace

foreach ($query->result_array() as $value) 
{
 echo $value['Kode_Kategori_Material_Jasa'];
 echo $value['Nama_Material_Jasa'];
}

with

if($query->num_rows()>0)
{            
  return $query->result();
}

Upvotes: 1

Rick Calder
Rick Calder

Reputation: 18705

Just reread your code. You're returning $value but never populating it. See below.

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

    if($query->num_rows()>0)
    {            
        $value = $query->result(); // or result_array() but you're using it as an object in the view not an array.
        return $value;
    }
    else
    {
        return null;
    }
}

Upvotes: 1

The Alpha
The Alpha

Reputation: 146201

In your model

if($query->num_rows()>0)
{            
    foreach ($query->result_array() as $value) {
        echo $value['Kode_Kategori_Material_Jasa'];
        echo $value['Nama_Material_Jasa'];
    }
    return $value;
}

it's wrong, it should be

if($query->num_rows()>0)
{            
    return $query->result();
}

and in your controller you should have

function index()
{
  $this->load->model('m_kategorimaterial'); // load it if it's not autoloaded
  $data['kirim'] = $this->m_kategorimaterial->read();
  $this->load->view('v/vkategorimaterial',$data);
}

Upvotes: 4

Related Questions