1

I'm newbie for codeigniter & PHP. need some help.
I've a trouble when trying to show my blog post into the blog view.
Table :

   blog        category      blog_category
| ------- |  | -------- |  | ------------- |
| blog_id |  |  cat_id  |  |     cat_id    |
| title   |  | cat_name |  |    blog_id    |
| content |  | cat_slug |

in my blog controllers :

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);
    }
}

and in my blog model :

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;
}

and in my blog view :

<?php foreach ($blog as $item): ?>
<h3 class="post-title"><a href="#"><?php echo $item->title;?></a></h3>
<p class="category"><?php echo $item->categories;?></p>
<?php endforeach;?>

and when I try to access the view, it's show 2 Post with a same Record but with different categories. In my blog post have a 2 categories and show one by one, if in my post have 3 categories then result show 3 post with 3 categories show one by one. sorry for my bad english.

dungeon
  • 11
  • 3
  • show your table data there is needed to change in query not in logic of CI – PRANAV Dec 10 '15 at 09:30
  • In your model, you use ` $this->db->join('blog_category', 'blog_category.cat_id=category.cat_id');` where you have used category.cat_id although you have not declared `category` as an alias. Is there any error that you receive while accessing the page? – bIgBoY Dec 10 '15 at 09:34
  • Also in your controller, add `echo $this->db->last_query()` above `$this->load->view('main/blog', $this->data);` this line to get the actual query executed and add that to the question for better clarity. – bIgBoY Dec 10 '15 at 09:36
  • @Aman No error message, but the post show multiple cause have multiple categories.. – dungeon Dec 10 '15 at 09:40
  • @dungeon did you try to echo the query? – bIgBoY Dec 10 '15 at 09:46
  • @Aman yap, just print_r($this->data); and show all result without any error message. – dungeon Dec 10 '15 at 09:50
  • https://jsfiddle.net/v3g9umpj/ – dungeon Dec 10 '15 at 09:52

1 Answers1

1

Join is wrong,

$this->db->join('blog_category', 'blog_category.blog_id=blog.blog_id', 'left');
                ^// its blog_category
$this->db->join('blog_category', 'blog_category.cat_id=category.cat_id');

Then use,

$this->db->group_by("blog.blog_id");// removes duplicates.
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41