BDR530 BDR530 - 14 days ago 5
PHP Question

CodeIgniter - Single result

I'm trying to use CodeIgniter to create a web application that show some articles.
I extracted all articles from the DB, they are used as previews.
Now i want to create a page that show a single article.
The previews of the articles are shown in /articles/, i would like to show a single article in /articles/read/id

This is my code:

Controller

<?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()
{
//?????
}


Model

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

}

}


View

<?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>


How can I do? Thanks in advance!

Answer

Put a param ($id) your read($id) method In controller :

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

Create new method get_select_articles() in your model like bellow :

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

Create a new page articles-single.php for show single article data in view folder. I have used $article['article_details']; in articles-single.php for view article details. You will use article_details or some thinks like that as per your db field name.

<?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>
Comments