我只想用codeigniter在数据库中显示我的数据。
我的代码显示error 404 The page you requested was not found
。
模型
<?php
class Daftar_model extends CI_Model
{
public function __construct()
{
//connect ke database
$this->load->database();
}
public function get_pertanyaan()
{
$query = $this->db->get('pertanyaan_ts');
return $query->result_array();
}
}
?>
控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Daftar_controller extends CI_Controller {
public function index()
{
//echo "Hallo";
$this->load->model('administrator/pertanyaan/daftar_model');
$this->load->view('administrator/pertanyaan/daftar_view',$data);
$data["pertanyaan_ts"] = $this->daftarpertanyaan_model->get_pertanyaan;
}
查看
<html>
<head>
<title><?php echo $judul; ?></title>
</head>
<body>
<h1>Daftar User</h1>
<table border="1">
<thead>
<tr>
<th>Pertanyaan</th>
</tr>
</thead>
<tbody>
<?php
foreach($pertanyaan as $p){
?>
<tr>
<td><?php echo $p->pertanyaan; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
答案 0 :(得分:1)
一对夫妇改变你的控制器,你应该很好。
更改班级名称并使用example.com/daftar
class Daftar extends CI_Controller {
public function index()
{
//assign the page title
$data['judaul'] = "Daftar User";
$this->load->model('administrator/pertanyaan/daftar_model');
//you need to assign a value to $data before sending it to the view
$data["pertanyaan_ts"] = $this->daftar_model->get_pertanyaan;
$this->load->view('administrator/pertanyaan/daftar_view', $data);
}
您的模型返回一个数组,因此您需要使用数组语法$p['pertanyaan_ts']
访问发送到视图的数据,而不是对象语法$p->pertanyaan_ts
。修改后的视图文件
<html>
<head>
<title><?php echo $judul; ?></title>
</head>
<body>
<h1>Daftar User</h1>
<table border="1">
<thead>
<tr>
<th>Pertanyaan</th>
</tr>
</thead>
<tbody>
<?php
foreach($pertanyaan_ts as $p)
{
?>
<tr>
<td><?php echo $p['pertanyaan']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>