显示数据库
我有以下几点。显示数据库
控制器/ customers.php
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class customers extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function view($id) {
$this->load->model('customers');
$news = $this->customers->view_customer($id);
$data['title'] = $news['title'];
$data['body'] = $news['body'];
$this->load->view('customers_customer_view', $data);
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('customers_view', $data);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('dashboard', 'refresh');
}
}
?>
型号/ customer.php
<?php
class customers_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function view_customer($id) {
if($id != FALSE) {
$query = $this->db->get_where('news', array('id' => $id));
return $query->row_array();
}
else {
return FALSE;
}
}
}
?>
的意见/ customers_customer_view.php
<?php print $title; ?>
<?php print $body; ?>
我是很新的代码点火器,我有从网络学习本教程,无论我做什么我都无法获取加载root/customers时显示的数据库信息/ view/1
我得到的只是一个空白页。即使我改变视图以包括一些静态文本,它不会显示,从这我相信这是加载视图是错误的,但一切看起来都对我好。
请有人帮忙。
您写道:
$this->load->model('customers');
但模型文件名为:customer.php。 而类名是:customers_model。 请再检查一次。
我给你举个例子:
$this->load->model('customers');
你的模型文件必须是:customers.php。
而且你的类名必须是:类客户{}
我将模型更改为“class customer_model extends .....”,同时我将控制器更改为“$ this-> load-> model('customer');”而我仍然得到一个空白页 – 2014-10-22 12:20:19
我现在得到它:),我认为,当命名一个模型,我不得不用_model来完成它。它现在工作。 – 2014-10-22 12:24:07
打印导致地方 我的意思是控制器或模型。或试试print_r($ news); 也删除session_start(); 将模型名称重命名为customers_model – 2014-10-22 12:10:12