在CodeIgniter上选择Data Not Work?

Uch*_*sun 0 php codeigniter

我在CI上有MVC,没有显示错误,但无法得到结果.

这个剧本

控制器 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

?>
Run Code Online (Sandbox Code Playgroud)

型号 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;

    }

}

?>
Run Code Online (Sandbox Code Playgroud)

查看 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>
Run Code Online (Sandbox Code Playgroud)

结果只显示这样的HTML 在此输入图像描述

任何人都可以帮我解决这个问题吗?

我是非常新的使用CodeIgniter.我之前从未使用过Framework.

我非常感谢你的回答.

谢谢

Jen*_*enz 5

您将结果从数据库存储到tests此处的数组

$data['tests'] = $this->customer_model->tampil_data_customer();
Run Code Online (Sandbox Code Playgroud)

并尝试使用未定义的数组在视图中调用它$results.

试试这个.

  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>";
  }
  }
Run Code Online (Sandbox Code Playgroud)

在模型中,你可以像这样简单,

public function tampil_data_customer()
    {
       $query=$this->db->query('select * from customer');
       $results = $query->result();
       return $results;
    }
Run Code Online (Sandbox Code Playgroud)