Welcome Guest, Not a member yet? Register   Sign In
Working with CI, MVC
#1

[eluser]jecrs[/eluser]
Good day all!

I am just new working with CI and I found it great!

I started to work with blogs and i found this problem that makes me think a lot and maybe you guys has an easy solution on it.

In my blog index page i am showing list of all the blogs created at the same time i had also a link on "comment", to comment on a specific blog. My question would be on how to fetch the number of comments from each queried blogs, maybe just a number.

here is my controller name blogs
Code:
function Index(){
        $data['title'] = 'Welcome to CI';
        $data['header'] = 'Sample Blog Application...';
        $data['blog'] = $this->blogsModel->getAll();
        
        $this->load->view('blogsList',$data);
    }

and my model for getAll on blogsModel
Code:
function getAll(){
        $this->db->order_by('blog_created','desc');
        return $this->db->get('blogs');
    }

->how will i fetch the id within this function and call another function to count for the number of comment on the id get

// thnx
#2

[eluser]haileCI[/eluser]
if you want to get the number of comment,you should have a table in your database which recored your comment, when you insert a comment, then add 1 for the comment's number

get the commont's num:
$this->db->query("SELECT num FROM myartic WHERE id=$id");
#3

[eluser]InsiteFX[/eluser]
Code:
// needs comments_model to hold comments for $post_id
/*
--
-- Table structure for table `comments`
--

CREATE TABLE IF NOT EXISTS `comments` (
  `id`            int(11)            NOT NULL auto_increment,
  `name`        varchar(255)    NOT NULL,
  `email`        varchar(255)    NOT NULL,
  `body`        varchar(255)    NOT NULL,
  `post_id`        int(11)            NOT NULL,
  `pub_date`    datetime        NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
*/

    public function get_comments($post_id)
    {
        $data = array();

        $this->db->where('post_id', $post_id);
        $query = $this->db->get('comments');

        if ($query->num_rows() > 0)
        {
            foreach ($query->result_array() as $row)
            {
                $data[] = $row;
            }
        }

        $query->free_result();
        return $data;
    }

InsiteFX
#4

[eluser]jecrs[/eluser]
Of course i already have a table for comments on blog - "blog_comment"
I can already fetch for the comments when a user clicks on a particular blog with this code below(controller).
Code:
function comment(){
        // $this->load->form_validation();
        $blog_id = $this->uri->segment(3);

        $this->form_validation->set_rules('comment_name','Mentor', 'required');
        $this->form_validation->set_rules('comment_text','Content', 'required');

        $data['title'] = 'Welcome to CI';
        $data['header'] = 'Sample Blog Application...';
        $data['action'] = site_url('blogs/addComment/'.$this->uri->segment('3'));
        $data['blog'] = $this->blogsModel->getById($blog_id)->row();
        $data['blog_comment'] = $this->blogsModel->getCommentById($blog_id);

        $this->load->view('blogComment',$data);
    }

My problem would be on the index page were in there are many id involve when querying all those posted blog.
I want to know the logic on how to fetch all those blogs at the same time count the comments accompanied with each blog? (I can actually do this easily using native php)
#5

[eluser]haileCI[/eluser]
each blog has the comment_num whitch store in the blog table
so echo $blog->comment_num
#6

[eluser]jecrs[/eluser]
my query is not answered... anyways thnx to those who replied ,,,Smile
#7

[eluser]wh1tel1te[/eluser]
I will assume that your posts table is named "posts" and your comments table is named "comments".

Code:
$posts = $this->db
    ->select('posts.*, COUNT(comments.comment_id) total_comments')
    ->join('comments', 'comments.post_id = posts.post_id', 'left')
    ->group_by('posts.post_id')
    ->get('posts')
    ->result_array();

You now have "total_comments" available attached to each post in the array.
#8

[eluser]jecrs[/eluser]
Hi 'wh1tel1te', it seems to be complicated but i think it will work fine..just like a native php right

and you understood my point, thank you Smile your the man'




Theme © iAndrew 2016 - Forum software by © MyBB