Php CodeIgniter查看博客文章页面
大家好,我正在尝试创建一个博客。第一个主页没问题。。我正在从数据库中获取shrot描述和类别,但是…我在链接方面有问题: 这是我的控制器功能:Php CodeIgniter查看博客文章页面,php,codeigniter,Php,Codeigniter,大家好,我正在尝试创建一个博客。第一个主页没问题。。我正在从数据库中获取shrot描述和类别,但是…我在链接方面有问题: 这是我的控制器功能: public function index() { $this->load->model('Model_cats'); $data['posts'] = $this->Model_cats->getLivePosts(10); $data['cats'] = $this-&
public function index()
{
$this->load->model('Model_cats');
$data['posts'] = $this->Model_cats->getLivePosts(10);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = 'Welcome to Paul Harbuz Blog Spot!';
$data['main'] = 'public_home';
$this->load->vars($data);
$this->load->view('template', $data);
}
public function category($id)
{
$data['category'] = $this->Model_cats->getCategory($id);
$data['posts'] = $this->Model_cats->getAllPostsByCategory($id);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = $data['category']['name'];
$data['main'] = 'public_home';
$this->load->vars($data);
$this->load->view('template', $data);
}
public function post($id)
{
$data['post'] = $this->Model_cats->getPost($id);
$data['comments'] = $this->Model_cats->getComments($id);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = $data['post']['title'];
$data['main'] = 'public_post';
$this->load->vars($data);
$this->load->view('template');
}
这是我的模型函数:
function getTopCategories()
{
$this->db->where('parentid',0);
$query = $this->db->get('categories');
$data = array();
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[$row['id']] = $row['name'];
}
}
$query->free_result();
return $data;
}
function getLivePosts($limit)
{
$data = array();
$this->db->limit($limit);
$this->db->where('status', 'published');
$this->db->order_by('pubdate', 'desc');
$query = $this->db->get('posts');
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
function getCategory($id)
{
$data = array();
$this->db->where('id',$id);
$this->db->limit(1);
$query = $this->db->get('categories');
if($query->num_rows() > 0)
{
$data = $query->row_array();
}
$query->free_result();
return $data;
}
function getAllPostsByCategory($catid)
{
$data = array();
$this->db->where('category_id', $catid);
$this->db->where('status', 'published');
$query = $this->db->get('posts');
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
function getPost($id)
{
$data = array();
$this->db->where('id',$id);
$this->db->limit(1);
$query = $this->db->get('posts');
if ($query->num_rows() > 0)
{
$data = $query->row_array();
}
$query->free_result();
return $data;
}
在查看页面中,我有如下内容:
if ( count($posts) )
{
foreach ($posts as $key => $list)
{
echo '<h2>'.$list['title'].'</h2>';
echo auto_typography(word_limiter($list['body'], 200));
echo anchor('post/'.$list['id'],'read more >>');
}
echo '<br/><br/>';
}
if(计数($posts))
{
foreach($key=>$list)
{
回显“.$list['title']”;
回声自动排版(单词限制器($list['body'],200));
回音主播('post/'.$list['id'],'readmore>>');
}
回音“
”;
}
我正在url中获取帖子id,但是。。我不知道为什么找不到该页面。您必须将控制器名称添加到锚uri段
echo anchor('CONTROLLER/post/'.$list['id'],'read more >>');
有关此主题的更多信息,请参阅
如果您想要像http://example.com/post/123
然后必须将以下内容添加到应用程序/config/routes.php
文件中:
$route['post/(:num)'] = "CONTROLLER/post/$1";
有关路由的更多信息也可以在中找到。您必须将控制器名称添加到锚uri段
echo anchor('CONTROLLER/post/'.$list['id'],'read more >>');
有关此主题的更多信息,请参阅
如果您想要像http://example.com/post/123
然后必须将以下内容添加到应用程序/config/routes.php
文件中:
$route['post/(:num)'] = "CONTROLLER/post/$1";
有关路由的更多信息,请参见。否。Post是您的职责。控制器名称是函数
post
所在的类的名称。非常感谢!帮我很多忙@马文·拉贝诺。邮政是你的职责。控制器名称是函数post
所在的类的名称。非常感谢!帮我很多忙@马文·拉贝