• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Short blog posts while listing them on my blog!

#1
[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 Smile.

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">
  &lt;?PHP foreach($results as $row): ?&gt;
    <div class="article_results">
      <div class="top">            
        <div class="metatag"></div>
        <span>
          By &lt;link href="&lt;?= $row-&gt;member_email; ?&gt;"&gt;
               &lt;?= $row->member_name; ?&gt;
             &lt;/link&gt;
             On &lt;?= $row->post_date; ?&gt;
        </span>
      </div>
                        
      <h1>
        &lt;link href="&lt;?= base_url();?&gt;blog/article/&lt;?= $row-&gt;post_id; ?&gt;"&gt;
          &lt;?= $row->post_title; ?&gt;
        &lt;/link&gt;
      </h1>
                        
      <p>&lt;?= $row->post_content; ?&gt;</p>
    </div>
  &lt;?PHP endforeach; ?&gt;
</div>

My Controller
Code:
&lt;?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);
    }
}


Messages In This Thread
Short blog posts while listing them on my blog! - by El Forum - 01-13-2012, 05:21 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.