我有以下型号和控制器。数据库表页面包含id,title,content和slug。
Q1。为什么控制器中的行
$this->load->model("pagemodel", 'pages'); // Load the page model,
有'页面'吗?
是否将“pagemodel”命名为页面?
我看到一条线,
$page_data = $this->pages->fetch($page_slug); // Pull the page data from the database
使用网页。
型号代码
<?php
class Pagemodel extends Model
{
/**
* Constructor
*/
function Pagemodel()
{
parent::Model();
}
/**
* Return an array of pages — used in the manage view
*
* @access public
* @return array
*/
function pages()
{
$query = $this->db->query("SELECT * FROM `pages` ORDER BY `id` ASC");
return $query->result_array();
}
/**
* Return a list of a single page — used when editing a page
*
* @access public
* @param integer
* @return array
*/
function page($id)
{
$query = $this->db->query("SELECT * FROM `pages` WHERE `id` = '$id' LIMIT 1");
return $query->result_array();
}
/**
* Return an array of a page — used in the front end
*
* @access public
* @param string
* @return array
*/
function fetch($slug)
{
$query = $this->db->query("SELECT * FROM `pages` WHERE `slug` = '$slug'");
return $query->result_array();
}
/**
* Add a record to the database
*
* @access public
* @param array
*/
function add($data)
{
$this->db->query("INSERT INTO `pages` (`title`, `content`, `slug`) VALUES (".$this->db->$data['title'].", ".$this->db->$data['content'].", ".$this->db->escape($data['slug']).")");
}
/**
* Update a record in the database
*
* @access public
* @param integer
* @param array
*/
function edit($id, $data)
{
$this->db->query("UPDATE `pages` SET `title` = ".$this->db->escape($data['title']).", `content` = ".$this->db->escape($data['content']).", `slug` = ".$this->db->escape($data['slug'])." WHERE `id` = '$id'");
}
/**
* Remove a record from the database
*
* @access public
* @param integer
*/
function delete($id)
{
$this->db->query("DELETE FROM `pages` WHERE `id` = '$id'");
}
}
?>
控制器代码
<?php
class Page extends Application
{
function Page()
{
parent::Application();
$this->load->model("pagemodel", 'pages'); // Load the page model
}
function _view($page, $page_data)
{
$meta = array (
'meta_title' => 'title here',
'meta_keywords' => 'keywords here',
'meta_description' => 'description here',
'meta_robots' => 'all',
'extra_headers' => '
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
'
);
$data = array();
// add any data
// merge meta and data
$data = array_merge($data,$meta);
// load view with data
$this->load->view('header', $data);
$this->load->view($page, $page_data);
$this->load->view('footer');
}
function index()
{
$page_slug = $this->uri->segment('2'); // Grab the URI segment
if($page_slug === FALSE)
{
$page_slug = 'home'; //Change home if you change the name in the back-end
}
$page_data = $this->pages->fetch($page_slug); // Pull the page data from the database
if($page_data === FALSE)
{
show_404(); // Show a 404 if no page exists
}
else
{
$this->_view('index', $page_data[0]);
}
}
}
?>
答案 0 :(得分:3)
来自system / libraries / Loader.php
/**
* Model Loader
*
* This function lets users load and instantiate models.
*
* @access public
* @param string the name of the class
* @param string name for the model
* @param bool database connection
* @return void
*/
function model($model, $name = '', $db_conn = FALSE)
{
...
所以是的,第二个参数只是为模型设置一个'友好'名称,而不是必需的。在我看来,它增加了一层混乱。
答案 1 :(得分:0)
你可能是对的,尝试重新命名'pages',我打赌它不再可用了 $ this-&gt;控制器代码中的页面。