[eluser]Unknown[/eluser]
Hello, I'm new to codeigniter and trying to learn as much as i can about it for building my website! I know this is very easy to do when writing the code yourself but trying to do it in codeigniter is hard for me!
Problem
I am writing my blog from the ground up. As you can see i have managed to display the articles but unfortunately it is showing the whole post which is not very attractive! I would like to be able to shorten the content to a certain amount of words and then add a 'read me..' link that will take me into the selected article page!
My Code currently active on
i know its very basic code and has no checking if articles exist ill add all that in later .
Download Full Application
my blog url: pixelwarez(dot)com/blog
My Model
Code:
<?PHP
class Blogmodel extends CI_Model {
public function __construct()
{
// Call the Model constructor
parent::__construct();
}
//This function is used to collect the category's from the database
public function get_cats(){
$query = $this->db->order_by("cat_order");
$query = $this->db->get("blog_cats");
return $query->result();
}
// This is the function that gets the latest posts!
public function get_latest($limit){
$this->db->from('blog_posts');
$this->db->join('members','blog_posts.post_author = members.member_id');
$this->db->order_by('post_id','desc');
$results = $this->db->get('',$limit);
return $results->result();
}
// This is the function that gets the full article
public function get_article_by_id(){
$id = (int) $this->uri->segment(3, 0);
$this->db->from('blog_posts');
$this->db->where('blog_posts.post_id', $id);
$this->db->join('members','blog_posts.post_author = members.member_id');
$results = $this->db->get();
return $results->result();
}
// This function will get all articles for a selected category.
public function get_article_by_catagory(){
$id = (int) $this->uri->segment(3, 0);
$this->db->from('blog_posts');
$this->db->where('blog_posts.post_cat_id', $id);
$this->db->join('members','blog_posts.post_author = members.member_id');
$results = $this->db->get();
return $results->result();
}
public function get_article_comments(){
$id = (int) $this->uri->segment(3, 0);
$this->db->from('blog_comments');
$this->db->where('blog_comments.com_parent', $id);
$this->db->join('members','blog_comments.com_author = members.member_id');
$com_results = $this->db->get();
return $com_results->result();
}
}
?>
My View
Code:
<div class="container">
<?PHP foreach($results as $row): ?>
<div class="article_results">
<div class="top">
<div class="metatag"></div>
<span>
By <link href="<?= $row->member_email; ?>">
<?= $row->member_name; ?>
</link>
On <?= $row->post_date; ?>
</span>
</div>
<h1>
<link href="<?= base_url();?>blog/article/<?= $row->post_id; ?>">
<?= $row->post_title; ?>
</link>
</h1>
<p><?= $row->post_content; ?></p>
</div>
<?PHP endforeach; ?>
</div>
My Controller
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Blog extends CI_Controller {
public function index()
{
$this->load->helper('text');
$this->load->model('blogmodel');
$data['query'] = $this->blogmodel->get_cats();
$data['results'] = $this->blogmodel->get_latest('10');
$this->load->view('blog', $data);
}
public function article(){
$this->load->model('blogmodel');
$data['query'] = $this->blogmodel->get_cats();
$data['results'] = $this->blogmodel->get_article_by_id();
$data['com_results'] = $this->blogmodel->get_article_comments();
foreach($data['results'] as $row){
$data['page_title'] = $row->post_title;
}
$this->load->view('blog_article', $data);
}
public function catagory(){
$this->load->model('blogmodel');
$data['query'] = $this->blogmodel->get_cats();
$data['results'] = $this->blogmodel->get_article_by_catagory();
$this->load->view('blog', $data);
}
}