[eluser]swgj19[/eluser]
Model:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Post_model extends CI_Model {
// --------------------------------------------------------------------
/**
* __construct()
*
* Constructor PHP 5+ NOTE: Not needed if not setting values!
*
* @access public
* @return void
*/
public function __construct()
{
parent::__construct();
}
// --------------------------------------------------------------------
/**
* get_post()
*
* Description:
*
* @access public
* @param string
* @return void
*/
public function get_post($id)
{
$data = array();
$this->db->where('id', $id);
$this->db->limit(1);
$query = $this->db->get('posts');
if ($query->num_rows() > 0)
{
$data = $query->row_array();
}
$query->free_result();
return $data;
}
// --------------------------------------------------------------------
/**
* get_all_posts()
*
* Description:
*
* @access public
* @return void
*/
public function get_all_posts()
{
$data = array();
$query = $this->db->get('posts');
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
// --------------------------------------------------------------------
/**
* get_all_posts_category()
*
* Description:
*
* @access public
* @param string
* @return void
*/
public function get_all_posts_category($cat_id)
{
$data = array();
$this->db->where("category_id", $cat_id);
$this->db->where('status', 'published');
$query = $this->db->get('posts');
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
// --------------------------------------------------------------------
/**
* get_live_posts()
*
* Description:
*
* @access public
* @param string
* @return void
*/
public function get_live_posts($limit)
{
$data = array();
$this->db->limit($limit);
$this->db->where('status', 'published');
$this->db->order_by('pub_date', 'desc');
$query = $this->db->get('posts');
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
// --------------------------------------------------------------------
/**
* add_post()
*
* Description:
*
* @access public
* @return void
*/
public function add_post()
{
$now = date("Y-m-d H:i:s");
$data = array(
'title' => $this->input->post('title'),
'tags' => $this->input->post('tags'),
'status' => $this->input->post('status'),
'body' => $this->input->post('body'),
'category_id' => $this->input->post('category_id'),
'user_id' => $this->session->userdata('user_id'),
'pub_date' => $now,
);
$this->db->insert('posts', $data);
}
// --------------------------------------------------------------------
/**
* update_post()
*
* Description:
*
* @access public
* @return void
*/
public function update_post()
{
$data = array(
'title' => $this->input->post('title'),
'tags' => $this->input->post('tags'),
'status' => $this->input->post('status'),
'body' => $this->input->post('body'),
'category_id' => $this->input->post('category_id'),
'user_id' => $this->session->userdata('user_id'),
);
$this->db->where('id', $this->input->post('id'));
$this->db->update('posts', $data);
}
// --------------------------------------------------------------------
/**
* delete_post()
*
* Description:
*
* @access public
* @param string
* @return void
*/
public function delete_post($id)
{
$this->db->where('id', $id);
$this->db->delete('posts');
// this will delete - remark out or delete the top 2 lines
$this->db->delete('posts', array('id' => $id));
}
}
// ------------------------------------------------------------------------
/* End of file post_model.php */
/* Location: ./application/models/post_model.php */