[eluser]m4d3 Gun[/eluser]
[quote author="whitey5759" date="1256554312"]CI certainly allows you to load Models from inside other Models, so I suspect there is just a mistake in your code. Can you post your code?[/quote]
thank's for ur replay
this is my code :
in my controller like bellow...
Code:
<?
class Blog extends Controller{
protected $_template;
public function __construct(){
parent::Controller();
$this->load->model('mdl_blog');
}
public function index($page = NULL){
$this->load->library('pagination');
$config['total_rows'] = $this->mdl_blog->get_posts_count();
$config['per_page'] = $this->mdl_blog->get_posts_per_page();
$this->pagination->initialize($config);
$pages_count = ceil($config['total_rows'] / $config['per_page']);
$page = ($page == 0) ? 1 : $page;
$offset = $config['per_page'] * ($page - 1);
if($data['posts'] = $this->mdl_blog->get_posts($config['per_page'],$offset)){
$data['posts_per_page'] = $this->mdl_blog->get_posts_per_page();
$data['posts_count'] = $this->mdl_blog->get_posts_count();
$data['pages_count'] = $pages_count;
$data['current_page'] = $page;
$data['next_page'] = $page + 1;
$data['previous_page'] = $page - 1;
foreach ($data['posts'] as $key => $post)
{
$data['posts'][$key]['url'] = post_url($post['url_title'], $post['date_posted']);
//$data['posts'][$key]['display_name'] = $this->users->get_user_display_name($post['author']);
}
$this->_template['page'] = 'blog/posts';
}
else
{
$this->_template['page'] = 'errors/no_posts';
}
$this->system_library->load_template($this->_template['page'], $data);
}
}
?>
mdl_blog
Code:
<?
class Mdl_blog extends Model{
public function __construct(){
parent::Model();
$this->load->model('mdl_categories');
}
public function get_posts_per_page(){
$this->db->select('value');
$this->db->where('name','posts_per_page');
$q = $this->db->get('settings',1);
if($q->num_rows()== 1){
$row = $q->row_array();
}
return $row['value'];
}
public function get_posts_count(){
$this->db->select('id');
$this->db->where('status','published');
$q = $this->db->count_all_results('posts');
return $q;
}
public function get_post_categories($post_id){
$this->db->select('category.id');
$this->db->where('post_id',$post_id);
$q = $this->db->get('post2cat');
if($q->num_rows()>0){
$result = $q->result_array();
foreach($result as $category){
$categories[] = $category['category_id'];
}
return $categories;
}
}
function get_posts($number = 5, $offset = 0){
$current_date = date('Y-m-d');
$this->db->select('posts.id, posts.author, posts.date_posted, posts.title, posts.url_title, posts.excerpt, posts.content, posts.allow_comments, posts.sticky, posts.status, posts.author, users.display_name');
$this->db->from('posts');
$this->db->join('users','posts.author = users.id');
$this->db->where('posts.status','published');
$this->db->where('posts.date_posted <=' .$current_date);
$this->db->order_by('sticky','DESC');
$this->db->order_by('id','DESC');
$q = $this->db->get();
if($q->num_rows()>0){
$result = $q->result_array();
foreach(array_keys($result)as $key){
$result[$key]['categories'] = $this->mdl_categories->get_categories_by_id($this->get_post_categories($result[$key]['id']));
}
return $result;
}
}
}
?>
and mdl_categories
Code:
<?
class Mdl_categories extends Model{
public function __construct(){
parent::Model();
}
public function get_categories(){
$this->db->select('id,name,url_name,description');
$q = $this->db->get('categories');
if($q->num_rows()>0){
return $q->result_array();
}
}
public function get_categories_by_id($cat_id){
$this->db->select('id,name,url_name,description');
$this->db->where_in('id',$cat_id);
$q = $this->db->get('categories');
if($q->num_rows()>0){
return $q->result_array();
}
}
public function get_categories_by_post($post_id){
$this->db->select('categories.name');
$this->db->join('post2cat','categories.id = post2cat.category_id');
$this->db->where('post_id',$post_id);
$q = $this->db->get('post2cat');
if($q->num_rows()>0){
return $q->result_array();
}
}
}
?>