新闻系统CodeIgniter

时间:2012-10-30 21:10:37

标签: php codeigniter url-routing

我尝试从http://ellislab.com/codeigniter/user_guide/tutorial/news_section.html

添加示例

到我的网站,这是代码:

news_model

<?php
class News_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
    }


public function get_news($art = FALSE)
{
    if ($art === FALSE)
    {
        $query = $this->db->get('news');
        return $query->result_array();
    }

    $query = $this->db->get_where('news', array('art' => $art));
    return $query->row_array();
}

public function set_news()
{
    $this->load->helper('url');

    $slug = url_title($this->input->post('title'), 'dash', TRUE);

    $data = array(
        'title' => $this->input->post('title'),
        'art' => $art,
        'text' => $this->input->post('text'),
        'image'=> $image
    );

    return $this->db->insert('news', $data);
  }
}
?>

news_controller

<?php
class News extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('news_model');
    }

    public function index()
    {
        $session_data = $this->session->userdata('logged_in');
        $data['username'] = $session_data['username'];
        $data['errors_login'] = array();
        $data['news'] = $this->news_model->get_news();
        $data['title'] = 'News archive';

    $this->load->view('main/open_news', $data);
    }

    public function view($art) {
    $data['news_item'] = $this->news_model->get_news($art);

    if (empty($data['news_item']))
    {
        show_404();
    }

    $data['title'] = $data['news_item']['title'];
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = $session_data['username'];
    $data['errors_login'] = array();
    $this->load->view('main/open_one_news', $data);
    }   
}

open_news

<?php
$this->load->view('mains/header');
$this->load->view('main/news');
$this->load->view('mains/footer');
?>

新闻观点

<?php foreach ($news as $news_item): ?>

    <h2><?php echo $news_item['title'] ?></h2>
    <div id="main">
        <?php echo $news_item['text'] ?>
    </div>
    <a href="news/<?php $news_item['art'] ?>">View article</a>
<?php endforeach ?>

当我点击<a href="news/<?php $news_item['art'] ?>">View article</a>

页面不会转发到带有新闻的混凝土页面,只能链接复制“新闻”:

http://localhost/index.php/news/news/news/news/news/news

我不知道什么是问题。但我认为它可以在routes.config中使用 因为我只有: $route['default_controller'] = 'login'; - &gt;这是我的开始页面

但在CodeIgniter教程中是:

$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

但是当我从第3行添加一些内容时,即使是包含新闻列表的第一页也不会打开。

解决: 我犯了愚蠢的错误。因为控制器名称是新闻,但功能:公共功能view($art),链接应为:'news/view/'.$news_item['art']

2 个答案:

答案 0 :(得分:1)

我认为以下链接存在问题

 <a href="news/<?php $news_item['art'] ?>">View article</a>

使用codeigniter锚而不是

anchor('news/'.$news_item['art'],'View article');

试试这个并喂我回来

答案 1 :(得分:0)

您忘记了echo at:

<a href="news/<?php $news_item['art'] ?>">View article</a> 

您应该使用:

<a href="news/<?php echo $news_item['art'] ?>">View article</a> 

或:

<?php echo anchor('news/' . $news_item['art'], 'View article');

或:

<?php echo anchor("news/{$news_item['art']}", 'View article');