Welcome Guest, Not a member yet? Register   Sign In
undefined index
#12

[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 */


Messages In This Thread
undefined index - by El Forum - 11-27-2012, 01:53 AM
undefined index - by El Forum - 11-27-2012, 01:54 AM
undefined index - by El Forum - 11-27-2012, 04:12 AM
undefined index - by El Forum - 11-27-2012, 04:44 AM
undefined index - by El Forum - 11-27-2012, 05:06 AM
undefined index - by El Forum - 11-27-2012, 05:07 AM
undefined index - by El Forum - 11-27-2012, 05:35 AM
undefined index - by El Forum - 11-27-2012, 05:44 AM
undefined index - by El Forum - 11-27-2012, 05:56 AM
undefined index - by El Forum - 11-27-2012, 06:07 AM
undefined index - by El Forum - 11-27-2012, 06:21 AM
undefined index - by El Forum - 11-27-2012, 06:23 AM
undefined index - by El Forum - 11-27-2012, 07:49 AM
undefined index - by El Forum - 11-27-2012, 09:30 AM
undefined index - by El Forum - 11-27-2012, 09:39 AM
undefined index - by El Forum - 11-27-2012, 10:08 AM
undefined index - by El Forum - 11-27-2012, 01:18 PM



Theme © iAndrew 2016 - Forum software by © MyBB