Welcome Guest, Not a member yet? Register   Sign In
Pass and id for a function from view to controller to model
#1

(This post was last modified: 09-15-2019, 08:44 AM by tp45.)

Hello 
am trying to select comments for a specific post but i can't figure it out because in my view i don't know if i can pass the post id like this

foreach($comments($post['post_id']) as $comment){
    // comments
}

public function getComments(//or maby $id){
   $data['comments'] = $this->post_model->commentForPost($id);
}

anyway here is my code: 

Model:
PHP Code:
public function get_comments($pid){
        
$this->db->select('*,
                            users.image AS profile_pic'
);
        
$this->db->from('comment');
        
$this->db->join('users''users.user_id = comment.user_id');
        
$this->db->where('comment_closed'0);
        
$this->db->where('post_id'$pid);
        
$this->db->order_by('commented_at''DESC');
        
$query $this->db->get();
        return 
$query->result_array();
    } 
Controller:
PHP Code:
    public function index(){
            
            
$data['posts'] = $this->Post_model->get_posts();
            
$data['comments'] = $this->Post_model->get_comments($pid);
            
//die(print_r($data['posts']['post_id],true));
            
$this->load->view('templates/header');
            
$this->load->view('posts/index'$data);
            
$this->load->view('templates/footer');
                      
//if i take an id from here like this $pid = $data['posts']['post_id] it doesn't work.
        

View:
PHP Code:
but i dont know how to pass post id
      
<?php foreach ($posts as $post): ?>
        <?php foreach ($comments as $comment): ?>
            <?php //my codeto display all comments ?>
        <?php endforeach ?>
      <?php endforeach ?>

Please help
Thanks in advance
Reply
#2

(This post was last modified: 09-15-2019, 11:20 AM by jreklund.)

Personally I would modify get_posts() so that it returns the following:
Code:
$data['posts'] = array(
    'data' => array(
        // all posts
    ),
    'ids' => array(
        // all posts ids
    )
)

// Change where to where_in for $pid
$data['comments'] = $this->Post_model->get_comments($posts['ids']);

// $data['comments']
array(
    post_id => data
)

# Where post_id are the actual id
array(
    1 => data,
    2 => data,
    3 => data
)

In your loop you can now do:
Code:
<?php foreach ($posts['data'] as $post): ?>
    <?php foreach ($comments[$post['post_id']] as $comment): ?>
        <?php //my codeto display all comments ?>
    <?php endforeach ?>
<?php endforeach ?>

https://codeigniter.com/user_guide/datab...sults.html
Use row_array() to create these arrays.
Reply
#3

(09-15-2019, 09:15 AM)jreklund Wrote: Personally I would modify get_posts() so that it returns the following:
Code:
$data['posts'] = array(
    'data' => array(
        // all posts
    ),
    'ids' => array(
        // all posts ids
    )
)

// Change where to where_in for $pid
$data['comments'] = $this->Post_model->get_comments($posts['ids']);

// $data['comments']
array(
    post_id => data
)

# Where post_id are the actual id
array(
    1 => data,
    2 => data,
    3 => data
)

In your loop you can now do:
Code:
<?php foreach ($posts['data] as $post): ?>
    <?php foreach ($comments[$post['post_id']] as $comment): ?>
        <?php //my codeto display all comments ?>
    <?php endforeach ?>
<?php endforeach ?>

https://codeigniter.com/user_guide/datab...sults.html
Use row_array() to create these arrays.

so to get all posts i should do something like this : 

PHP Code:
public function get_posts(){
        
$this->db->select('*,
                            users.image AS profile_pic,
                            post.image AS post_image'
);
        
$this->db->from('post');
        
$this->db->join('users''users.user_id = post.user_id');
        
$this->db->where('post_closed'0);
        
$this->db->order_by('posted_at''DESC');
        
$query $this->db->get();
        return 
$query->row_array(); //$query->result_array();
    

Reply
#4

(This post was last modified: 09-15-2019, 11:23 AM by jreklund.)

No, like this. I didn't read the docs that close so I said the wrong function.
PHP Code:
public function get_posts(){
    
$this->db->select('*,
                        users.image AS profile_pic,
                        post.image AS post_image'
);
    
$this->db->from('post');
    
$this->db->join('users''users.user_id = post.user_id');
    
$this->db->where('post_closed'0);
    
$this->db->order_by('posted_at''DESC');
    
$query $this->db->get();
    
$data = [];
    
    while(
$row $query->unbuffered_row('array'))
    {
        
$data['data'][] = $row;
        
$data['ids'][] = $row['post_id'];
    }
    
    return 
$data;


And for get_comments()
PHP Code:
public function get_comments($pid){
    
$this->db->select('*,
                        users.image AS profile_pic'
);
    
$this->db->from('comment');
    
$this->db->join('users''users.user_id = comment.user_id');
    
$this->db->where('comment_closed'0);
    
$this->db->where_in('post_id'$pid);
    
$this->db->order_by('commented_at''DESC');
    
$query $this->db->get();
    
$data = [];
    
    while(
$row $query->unbuffered_row('array'))
    {
        
$data[$row['post_id']][] = $row;
    }
    
    return 
$data;

Reply
#5

(This post was last modified: 09-15-2019, 12:37 PM by tp45.)

(09-15-2019, 11:17 AM)jreklund Wrote: No, like this. I didn't read the docs that close so I said the wrong function.
PHP Code:
public function get_posts(){
    
$this->db->select('*,
                        users.image AS profile_pic,
                        post.image AS post_image'
);
    
$this->db->from('post');
    
$this->db->join('users''users.user_id = post.user_id');
    
$this->db->where('post_closed'0);
    
$this->db->order_by('posted_at''DESC');
    
$query $this->db->get();
    
$data = [];
    
    while(
$row $query->unbuffered_row('array'))
    {
        
$data['data'][] = $row;
        
$data['ids'][] = $row['post_id'];
    }
    
    return 
$data;


And for get_comments()
PHP Code:
public function get_comments($pid){
    
$this->db->select('*,
                        users.image AS profile_pic'
);
    
$this->db->from('comment');
    
$this->db->join('users''users.user_id = comment.user_id');
    
$this->db->where('comment_closed'0);
    
$this->db->where_in('post_id'$pid);
    
$this->db->order_by('commented_at''DESC');
    
$query $this->db->get();
    
$data = [];
    
    while($row $query->unbuffered_row('array'))
    {
        $data[$row['post_id']][] = $row;
    }
    
    return 
$data;

Thank you very much @jreklund.  i will let you know about the results.

(09-15-2019, 11:35 AM)tp45 Wrote:
(09-15-2019, 11:17 AM)jreklund Wrote: No, like this. I didn't read the docs that close so I said the wrong function.
PHP Code:
public function get_posts(){
    
$this->db->select('*,
                        users.image AS profile_pic,
                        post.image AS post_image'
);
    
$this->db->from('post');
    
$this->db->join('users''users.user_id = post.user_id');
    
$this->db->where('post_closed'0);
    
$this->db->order_by('posted_at''DESC');
    
$query $this->db->get();
    
$data = [];
    
    while(
$row $query->unbuffered_row('array'))
    {
        
$data['data'][] = $row;
        
$data['ids'][] = $row['post_id'];
    }
    
    return 
$data;


And for get_comments()
PHP Code:
public function get_comments($pid){
    
$this->db->select('*,
                        users.image AS profile_pic'
);
    
$this->db->from('comment');
    
$this->db->join('users''users.user_id = comment.user_id');
    
$this->db->where('comment_closed'0);
    
$this->db->where_in('post_id'$pid);
    
$this->db->order_by('commented_at''DESC');
    
$query $this->db->get();
    
$data = [];
    
    while($row $query->unbuffered_row('array'))
    {
        $data[$row['post_id']][] = $row;
    }
    
    return 
$data;

Thank you very much @jreklund.  i will let you know about the results.

Sorry jreklund 
do i still have to do this ?

 <?php foreach ($comments[$post['post_id']] as $comment): ?>
        <?php //my codeto display all comments ?>
    <?php endforeach ?>
since i get the ids from controller $post['ids]

(09-15-2019, 11:35 AM)tp45 Wrote:
(09-15-2019, 11:17 AM)jreklund Wrote: No, like this. I didn't read the docs that close so I said the wrong function.
PHP Code:
public function get_posts(){
    
$this->db->select('*,
                        users.image AS profile_pic,
                        post.image AS post_image'
);
    
$this->db->from('post');
    
$this->db->join('users''users.user_id = post.user_id');
    
$this->db->where('post_closed'0);
    
$this->db->order_by('posted_at''DESC');
    
$query $this->db->get();
    
$data = [];
    
    while(
$row $query->unbuffered_row('array'))
    {
        
$data['data'][] = $row;
        
$data['ids'][] = $row['post_id'];
    }
    
    return 
$data;


And for get_comments()
PHP Code:
public function get_comments($pid){
    
$this->db->select('*,
                        users.image AS profile_pic'
);
    
$this->db->from('comment');
    
$this->db->join('users''users.user_id = comment.user_id');
    
$this->db->where('comment_closed'0);
    
$this->db->where_in('post_id'$pid);
    
$this->db->order_by('commented_at''DESC');
    
$query $this->db->get();
    
$data = [];
    
    while($row $query->unbuffered_row('array'))
    {
        $data[$row['post_id']][] = $row;
    }
    
    return 
$data;

Thank you very much @jreklund.  i will let you know about the results.

(09-15-2019, 11:35 AM)tp45 Wrote:
(09-15-2019, 11:17 AM)jreklund Wrote: No, like this. I didn't read the docs that close so I said the wrong function.
PHP Code:
public function get_posts(){
    
$this->db->select('*,
                        users.image AS profile_pic,
                        post.image AS post_image'
);
    
$this->db->from('post');
    
$this->db->join('users''users.user_id = post.user_id');
    
$this->db->where('post_closed'0);
    
$this->db->order_by('posted_at''DESC');
    
$query $this->db->get();
    
$data = [];
    
    while(
$row $query->unbuffered_row('array'))
    {
        
$data['data'][] = $row;
        
$data['ids'][] = $row['post_id'];
    }
    
    return 
$data;


And for get_comments()
PHP Code:
public function get_comments($pid){
    
$this->db->select('*,
                        users.image AS profile_pic'
);
    
$this->db->from('comment');
    
$this->db->join('users''users.user_id = comment.user_id');
    
$this->db->where('comment_closed'0);
    
$this->db->where_in('post_id'$pid);
    
$this->db->order_by('commented_at''DESC');
    
$query $this->db->get();
    
$data = [];
    
    while($row $query->unbuffered_row('array'))
    {
        $data[$row['post_id']][] = $row;
    }
    
    return 
$data;

Thank you very much @jreklund.  i will let you know about the results.

Sorry jreklund 
do i still have to do this ?

 <?php foreach ($comments[$post['post_id']] as $comment): ?>
        <?php //my codeto display all comments ?>
    <?php endforeach ?>
since i get the ids from controller $post['ids]

Am sorry jreklund but am messing up i can see the data but i just can't load it to the arrays

PHP Code:
        public function index(){
            
            
$data['post_data'] = $this->Post_model->get_posts();
            
//when i print this it shows me all the data, i cant hack the arrays
            
die(print_r($data['post_data'],true));

            
$data['posts'] = array(
                
'data' => array(
                    
'post_id' => $data['post_data']['post_id'],
                    
'users_id' => $data['post_data']['users_id'], 
                    
'post' => $data['post_data']['post'], 
                    
'posted_at' => $data['post_data']['posted_at'], 
                ),
                
'ids' => array(
                    
'post_id' => $data['post_data']['post_id'],
                )
            );
            
            
$data['comments'] = $this->Post_model->get_comments($posts['ids']);
            
//die(print_r($data['posts'],true));
            
$this->load->view('templates/header');
            
$this->load->view('posts/index'$data);
            
$this->load->view('templates/footer');

        } 
I dont know if am going the right way, i get undefined index error when i try to print $posts['data'];

ok i see this has all data $data['post_data']['data'] and this $data['post_data']['ids'] has ids obly
Reply
#6

(This post was last modified: 09-15-2019, 01:30 PM by tp45.)

Ok i did this :


PHP Code:
        public function index(){
            
            
$data['post_data'] = $this->Post_model->get_posts();

            
$data['posts'] = array(
                
'data' => array(
                    
'data' => $data['post_data']['data'],
                ),
                
'ids' => array(
                    
'post_id' => $data['post_data']['ids'],
                )
            );

            
$data['comments'] = $this->Post_model->get_comments($data['posts']['ids']);
            
//die(print_r($data['comments'],true));
            //die(print_r($data['posts'],true));
            
$this->load->view('templates/header');
            
$this->load->view('posts/index'$data);
            
$this->load->view('templates/footer');

        } 

 i get this error 
Code:
A PHP Error was encountered
Code:
Severity: Notice
Code:
Message: Array to string conversion
Code:
Filename: database/DB_query_builder.php
Code:
Line Number: 837
Code:
Backtrace:
Code:
File: C:\xampp\htdocs\friendmiiDemo\application\models\Post_model.php
Line: 280
Function: where_in

Error Number: 42S22/207

[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'Array'.

SELECT *, "users"."image" AS "profile_pic" FROM "comment" JOIN "users" ON "users"."user_id" = "comment"."user_id" WHERE "comment_closed" = 0 AND "post_id" IN(Array) ORDER BY "commented_at" DESC

Filename: C:/xampp/htdocs/friendmiiDemo/system/database/DB_driver.php

Line Number: 691

(09-15-2019, 01:03 PM)tp45 Wrote: Ok i fixed it like this:

PHP Code:
public function index(){
            
            
$data['post_data'] = $this->Post_model->get_posts();

            
$data['posts'] = array(
                
'data' => $data['post_data']['data'],
                
'post_id' => $data['post_data']['ids'],
            );
            
//die(print_r($data['posts']['post_id'],true));
            
$data['comments'] = $this->Post_model->get_comments($data['posts']['post_id']);
            
            
//die(print_r($data['comments'],true));
            
$this->load->view('templates/header');
            
$this->load->view('posts/index'$data);
            
$this->load->view('templates/footer');

    } 
Again Thank you so much  jreklund

This is my System pat of it :https://www.dropbox.com/s/c8vb7swwzighv8j/Screenshot%20%2858%29.png?dl=0
Reply
#7

(This post was last modified: 09-15-2019, 10:08 PM by Wouter60.)

In your view, it looks like you want to display all posts with all their comments, is that correct?
Anyway, you'll need JOIN to get the posts and comments in one query.
Reply
#8

(This post was last modified: 09-16-2019, 10:08 AM by jreklund.)

@tp45: You don't need to do this:
Code:
$data['posts'] = array(
   'data' => $data['post_data']['data'],
   'post_id' => $data['post_data']['ids'],
);

As it's already in that format.

Just do this:
Code:
$data['posts'] = $this->Post_model->get_posts();
$data['comments'] = $this->Post_model->get_comments($data['posts']['ids']);
Reply
#9

I repeat: what's the use of getting all posts from your database, and just the comments for one specfic post?
Why don't you use JOIN to get the post details and it's comments in one query? You can do that for all posts at once, or for one post, based on it's id.
Reply
#10

(This post was last modified: 09-17-2019, 08:31 AM by tp45.)

(09-16-2019, 10:08 AM)jreklund Wrote: @tp45: You don't need to do this:
Code:
$data['posts'] = array(
   'data' => $data['post_data']['data'],
   'post_id' => $data['post_data']['ids'],
);

As it's already in that format.

Just do this:
Code:
$data['posts'] = $this->Post_model->get_posts();
$data['comments'] = $this->Post_model->get_comments($data['posts']['ids']);
Ok Thanks

@Wouter60 

Its a good solution Thank you.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB