0

我尝试添加示例http://ellislab.com/codeigniter/user_guide/tutorial/news_section.html

到我的网站,这是代码:

新闻模型

<?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);
  }
}
?>

新闻控制器

<?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);
    }   
}

开放新闻

<?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';-> 这是我的起始页

但在 CodeIgniter 教程中是:

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

但是当我从 3 第一行添加一些时,即使是带有新闻列表的第一页也没有打开。

已解决:我犯了愚蠢的错误。因为控制器名称是 news,但是 function: public function view($art),而链接应该是: 'news/view/'.$news_item['art'].

4

2 回答 2

1

我认为以下链接有问题

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

改用 codeigniter 锚

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

试试这个并反馈给我

于 2012-10-30T21:53:24.370 回答
0

你忘了echo在:

<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');
于 2012-10-30T21:56:55.647 回答