Таблица:
категория блога blog_category
| ------- | | -------- | | ------------- |
| идентификатор блога | | кот_ид | | cat_id |
| титул | | имя_кошки | | blog_id |
| контент | | cat_slug |
в контроллерах моего блога:
Код: Выделить всё
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller
{
/*
* Blog Controller
*/
public function __construct()
{
parent::__construct();
$this->load->model('blog_model');
}
public function index()
{
$this->data = array(
'title' => 'Blog',
'blog' => $this->blog_model->get_all_post(),
);
$this->load->view('main/blog', $this->data);
}
}
Код: Выделить всё
function get_all_post() {
$this->db->select('*');
$this->db->from('blog');
$this->db->join('blog_category', 'blog_category.blog_id=blog.blog_id', 'left');
$this->db->join('category', 'blog_category.cat_id=category.cat_id');
$query = $this->db->get();
$select = array();
foreach($query->result() as $row) {
$select[] = $row;
}
if (count($select) > 0)
return $select;
return NULL;
}
Код: Выделить всё
[url=#][/url]
Подробнее здесь: https://stackoverflow.com/questions/341 ... lationship
Мобильная версия