CodeIgniter - 单个结果

时间:2016-11-14 14:05:39

标签: php codeigniter

我尝试使用CodeIgniter创建一个显示某些文章的Web应用程序。 我从数据库中提取了所有文章,它们被用作预览。 现在我想创建一个显示单篇文章的页面。 这些文章的预览显示在 / articles / 中,我想在 / articles / read / id

中显示一篇文章

这是我的代码:

控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Articles extends CI_Controller
{

    public function index() 
    {             
        $this->load->model('articles_model');

        $articles_data['data'] = $this->articles_model->select_articles();

        $this->load->view('header');

        $this->load->view('nav');

        $this->load->view('articles-content', $articles_data);

        $this->load->view('footer');

    }

    public function read()
    {
        //?????
    }

模型

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Articles_model extends CI_Model 
{

    public function select_articles()
    {
        $this->db->select('*');
        $this->db->from('articles');
        $this->db->join('authors', 'articles.id_author = authors.id_author', 'left');
        $this->db->join('images', 'articles.id_image = images.id_image', 'left');
        $query = $this->db->get();
        return $query->result_array();

    }

}

查看

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div id="content">
   <div id="articles">
      <?php foreach($data as $row){
         $limited_article = word_limiter($row['article_text'], 200);
      ?>
      <section class="article">
         <h2><?php echo $row['article_title'];?></h2>
         <img class="article-image" src="<?php echo base_url('/assets/images/' . $row['image']) ;?>">
         <p class="info">Author: <?php echo $row['author']; ?> Date: <?php echo $row['article_data'] ?></p>
         <p><?php echo $limited_article; ?></p>
      </section>
      <?php }?>
   </div>
</div>

我该怎么办?提前谢谢!

1 个答案:

答案 0 :(得分:0)

将一个参数($id)设置为read($id)方法在控制器中:

public function read($id) {
    $this->load->model('articles_model');

    $data['article'] = $this->articles_model->get_select_articles($id);

    $this->load->view('header');
    $this->load->view('nav');
    $this->load->view('articles-single', $data);
    $this->load->view('footer');
}

在模型中创建新方法get_select_articles(),如下所示:

class Articles_model extends CI_Model 
{
    public function get_select_articles($id){
        $this->db->select('*');
        $this->db->from('articles');
        $this->db->join('authors', 'articles.id_author = authors.id_author', 'left');
        $this->db->join('images', 'articles.id_image = images.id_image', 'left');
        $this->db->where('articles.id', $id);
        $query = $this->db->get();
        return $query->row_array();
    }
}

为在视图文件夹中显示单篇文章数据创建新页面articles-single.php。我在$article['article_details'];中使用了articles-single.php来查看文章详细信息。您将根据您的数据库字段名称使用article_details或某些人认为这样。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div id="content">
   <div id="articles">
      <section class="article">
            <?php if($article) { ?>
            <h2><?php echo $article['article_title'];?></h2>
            <img class="article-image" src="<?php echo base_url('/assets/images/' . $article['image']) ;?>">
            <p class="info">Author: <?php echo $article['author']; ?> Date: <?php echo $article['article_data'] ?></p>
            <p><?php echo $article['article_details']; ?></p>
            <?php } ?>
      </section>
      <?php }?>
   </div>
</div>