Welcome Guest, Not a member yet? Register   Sign In
DIsplay Category in Post
#1

Hello, I'm new to codeigniter, am creating a small website that as a blog with 1 to many relationship, with the help of tutorial on creating MY_Model, of which i did, and now i want to display category to each post.

this is my post_m.php

<?php
class Post_m extends MY_Model
{
   protected $_table_name = 'posts';
   protected $_order_by = 'pubdate desc, id desc';
   protected $_timestamps = TRUE;
   public $rules = array(
       'pubdate' => array(
           'field' => 'pubdate',
           'label' => 'Publication date',
           'rules' => 'trim|required|exact_length[10]'
       ),
       'title' => array(
           'field' => 'title',
           'label' => 'Title',
           'rules' => 'trim|required|max_length[100]'
       ),
       'slug' => array(
           'field' => 'slug',
           'label' => 'Slug',
           'rules' => 'trim|required|max_length[100]|url_title'
       ),
       'body' => array(
           'field' => 'body',
           'label' => 'Body',
           'rules' => 'trim|required'
       )
   );

   public function get_new ()
   {
       $post = new stdClass();
       $post->title = '';
       $post->slug = '';
       $post->body = '';
       $post->name = '';
       $post->post_image = '';
       $post->pubdate = date('Y-m-d');
       return $post;
   }

   public function set_published(){
       $this->db->where('pubdate <=', date('Y-m-d'));
   }

   public function get_recent($limit = 3){

       // Fetch a limited number of recent articles
       $limit = (int) $limit;
       $this->set_published();
       $this->db->limit($limit);
       return parent::get();
   }

   public function get_categories()
   {
       $this->db->order_by('name');
       $query = $this->db->get('categories');
       return $query->result_array();
   }

}
Reply
#2

This may help.

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

/**
 * Class Category_model
 */
class Category_model extends MY_Model
{
    
/**
     * @var  string - Categories table name.
     */
    
private $tableName 'categories';
    
    
/**
     * Category_model constructor.
     * -------------------------------------------------------------------
     *
     */
    
public function __construct()
    {
        
    }

    
// -------------------------------------------------------------------

    /**
     * getCategory ()
     * -------------------------------------------------------------------
     *
     * @param  $id
     * @return array
     */
    
public function getCategory($id)
    {
        
$data    = [];
        
$options = ['id' => $id];

        
$query $this->db->getwhere($this->tableName$options1);

        if (
$query->num_rows() > 0)
        {
            
$data[] = $query->row_array();
        }

        
$query->free_result();

        return 
$data;
    }

    
// -------------------------------------------------------------------

    /**
     * getAllCategories ()
     * -------------------------------------------------------------------
     *
     * @return array
     */
    
public function getAllCategories()
    {
        
$data = [];

        
$query $this->db->get($this->tableName);

        if (
$query->num_rows() > 0)
        {
            foreach (
$query->result_array() as $row)
            {
                
$data[] = $row;
            }
        }

        
$query->free_result();

        return 
$data;
    }

    
// -------------------------------------------------------------------

    /**
     * getCategoriesDropDown ()
     * -------------------------------------------------------------------
     *
     * @return array
     */
    
public function getCategoriesDropDown()
    {
        
$data = [];

        
$query $this->db->select('id, title')
         
                 ->get($this->tableName);

        if (
$query->num_rows() > 0)
        {
            foreach (
$query->result_array() as $row)
            {
                
$data[$row['id']] = $row['title'];
            }
        }

        
$query->free_result();

        return 
$data;
    }

    
// -------------------------------------------------------------------

    /**
     * getTopCategories ()
     * -------------------------------------------------------------------
     *
     * @return mixed
     */
    
function getTopCategories()
    {
        
$data = array();

        
$query $this->db->where('parent_id'0)
         
                 ->get($this->tableName);

        if (
$query->num_rows() > 0)
        {
            foreach (
$query->result_array() as $row)
            {
                
$data[$row['id']] = $row['title'];
            }
        }

        
$query->free_result();

        return 
$data;
    }

    
// -------------------------------------------------------------------

    /**
     * addCategory ()
     * -------------------------------------------------------------------
     *
     */
    
function addCategory()
    {
        
$data = [
            
'title'      => str_replace(" ""_"$this->input->post('title'true)),
            
'short_desc' => $this->input->post('short_desc'true),
            
'long_desc'  => $this->input->post('long_desc'true),
            
'status'     => $this->input->post('status'true),
            
'sort_order' => $this->input->post('sort_order'true)
        ];

        
$this->db->insert($this->tableName$data);
    }

    
// -------------------------------------------------------------------

    /**
     * updateCategory ()
     * -------------------------------------------------------------------
     *
     */
    
function updateCategory()
    {
        
$data = [
            
'title'      => str_replace(" ""_"$this->input->post('title'true)),
            
'short_desc' => $this->input->post('short_desc'true),
            
'long_desc'  => $this->input->post('long_desc'true),
            
'status'     => $this->input->post('status'true),
            
'sort_order' => $this->input->post('sort_order'true)
        ];

        
$this->db->where('id'$this->input->post('id'true));
        
$this->db->update($this->tableName$data);

    }

    
// -------------------------------------------------------------------

    /**
     * deleteCategory ()
     * -------------------------------------------------------------------
     *
     * @param $id
     */
    
function deleteCategory($id)
    {
        
$data = ['status' => 'inactive'];

        
$this->db->where('id'$id)
         
        ->update($this->tableName$data);
    }

}

/**
 * -----------------------------------------------------------------------
 * Filename: Category_model.php
 * Location: ./application/models/Category_model.php
 * -----------------------------------------------------------------------
 */ 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB