0

I have MVC on CI that not show error but cant get the result.

This the Script

Controller customer.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Customer extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }
    
    function index()
    {
        $this->load->view('customer_view');
        
    }   

    function tampil_customer()
    {
         $this->load->model('customer_model');
    
         $data = array('tests' => $this->customer_model->tampil_data_customer());
         
         $this->load->view('customer_view', $data);
        
    }//end fucnntion
    
        

}//end class
 
?>

Model customer_model.php

<?php

class Customer_model extends CI_Models
{
    
    public function tampil_data_customer()
    {
       $results = array();
       $this->db->select('*');
       $this->db->from('customer');
       
        $query = $this->db->get();
        
        if($query->num_rows() > 0) 
        {
          $results = $query->result();
        }
        return $results;
            
    }
    
}
   
?>

View customer_view.php

<table border="1" align="center" widht="900">
  <tr>
    <td colspan="5" align="center"><h2>Data Customer</h2></td>
  </tr>
  <tr>
    <th>Nama</th>
    <th>Company</th>
    <th>Alamat</th>
    <th>Telepon</th>
    <th>HP</th>
  </tr>

<?php

//$result = $this->result(); 
if( !empty($results) )
{
  foreach($results as $isi) 
  {
    echo "<tr>";
      echo "<td>$isi->nama</td>";
      echo "<td>$isi->company</td>";
      echo "<td>$isi->alamat</td>";
      echo "<td>$isi->telepon</td>";
      echo "<td>$isi->hp</td>";
      //echo anchor('customer/edit_customer/'.$isi->ID, 'Edit')."|";
      //echo anchor('customer/hapus_customer/'.$isi->ID, 'Hapus')."|";
    echo "</tr>";
      
  }
}  
 

?>
</table>

the Result only show the HTML like this enter image description here

0

2 Answers 2

5

You are storing the result from database to array tests here

$data['tests'] = $this->customer_model->tampil_data_customer();

and trying to call it in view with an undefined array $results.

Try with this.

  if(!empty($tests->result())){
  foreach($tests->result() as $isi) 
  {
   echo "<tr>";
   echo "<td>".$isi->nama."</td>";
   echo "<td>".$isi->company."</td>";
   echo "<td>".$isi->alamat."</td>";
   echo "<td>".$isi->telepon."</td>";
   echo "<td>".$isi->hp."</td>";
   echo "</tr>";
  }
  }

In model can you make it simple like this,

public function tampil_data_customer()
    {
       $query=$this->db->query('select * from customer');
       $results = $query->result();
       return $results;
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Absolutely correct! In CI, the key in controller becomes variAble in view!
@Jenz I have Try your Advice($results change to $test), n Still Not Work. The Result still like the image upload
@Jenz oke, on first comment that my mistake. I have try $tests too but the result is same. Not show the data. :(, any Idea?
@Jenz I have Try All of your advice, I have check it repeatedly, and geting error Message: Undefined variable: tests.
try whether you are getting the result in controller page.
0

when you pass in arguments using $this->load->view() function,you are using the view() function with the instance of Loader load,it's in /system/core/Loader.php. As the block comments say

An associative array of data to be extracted for use in the view.

if you pass in an object,you can get the public properties by simply using the property name,you can't get the protected and private property.

if you pass in an Multi-dimensional Array,you can get the value by using key/keys in the Multi-dimensional Array.

By the way,you must pass in an array or object other a variable.

sample below

model.php(application\models\test)

class model extends CI_Model {
    public  $ss=1;
    protected  $bb=1;
    private  $vv=3;
}

controller.php(application\controllers)

class Hello extends CI_Controller
    public function __construct() {
        parent::__construct ();
    }
    public function index() {
        $this->load->model('test/model');
        $model=new model();
        $this->load->view ( 'test/view', $model );
    }

view.php(application\views\test)

echo $ss;

then you get the value of object.

1 Comment

hi @jayxhj Im Still not understand with your explanation. can you give me how to do that? Before This, I succes create CRUD on CI, and I confuse why in a second Try I cant Select The data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.