Welcome Guest, Not a member yet? Register   Sign In
Simple problem...
#1

[eluser]Medikal[/eluser]
Code:
$template['blogPosts'] = $this->blog_model->selectPosts();
        $template['comments'] = $this->blog_model->selectComments(2);

Self explanatory based on the code, I'm just trying to make the selectComments base itself on the postID retrieved by selectPosts, so instead of 2 it would be $template['blogPosts']->postID, although obviously that doesn't work.

Any ideas?
#2

[eluser]Atharva[/eluser]
[quote author="Medikal" date="1291740340"]
Code:
$template['blogPosts'] = $this->blog_model->selectPosts();
        $template['comments'] = $this->blog_model->selectComments(2);

Self explanatory based on the code, I'm just trying to make the selectComments base itself on the postID retrieved by selectPosts, so instead of 2 it would be $template['blogPosts']->postID, although obviously that doesn't work.

Any ideas?[/quote]

Can you post code for selectPosts function?
#3

[eluser]Medikal[/eluser]
Code:
function selectPosts()
            {
                $selectQuery = $this->db->select("*");
                $selectQuery = $this->db->from("posts");
                $selectQuery = $this->db->order_by("postedTime", "desc");
                
                $selectQuery = $this->db->get();
                
                if ($selectQuery->num_rows() > 0)
                    {
                        foreach ($selectQuery->result() as $row)
                            {
                                $posts[] = $row;
                            }
                    }
                    
                    return $posts;
            }

and comments...
Code:
function selectComments($postid)
            {    
            $comments=array();
                $selectQuery = $this->db->select("*");
                $selectQuery = $this->db->from("comments");
                $selectQuery = $this->db->where("parentID", $postid);
                
                $selectQuery = $this->db->get();
                
                if ($selectQuery->num_rows() > 0)
                    {
                        foreach ($selectQuery->result() as $row)
                            {
                                $comments[] = $row;
                            }
                    }
                
                return $comments;
            }
#4

[eluser]Atharva[/eluser]
[quote author="Medikal" date="1291740547"]
Code:
function selectPosts()
            {
                $selectQuery = $this->db->select("*");
                $selectQuery = $this->db->from("posts");
                $selectQuery = $this->db->order_by("postedTime", "desc");
                
                $selectQuery = $this->db->get();
                
                if ($selectQuery->num_rows() > 0)
                    {
                        return $selectQuery->row();
                    }
                    else return null;

                    
            }

This will work assuming that you want to select one record from query.
#5

[eluser]Medikal[/eluser]
I am using it to select all posts from the database, the main issue is having my selectComments function be able to refer to each id in the selectPosts.
#6

[eluser]Atharva[/eluser]
[quote author="Medikal" date="1291741010"]I am using it to select all posts from the database, the main issue is having my selectComments function be able to refer to each id in the selectPosts.[/quote]

Ok, so in that case, you need to keep your previous code in model and loop in the controller like
Code:
foreach($template['blogPosts'] as $val){
$template['comments'] = $this->blog_model->selectComments($val->postID);
}

have you tried this?

Of course, $template['comments'] will get overwritten each time in loop, so you need to store in different way.
#7

[eluser]Atharva[/eluser]
You can also consider joining two queries into one which will save you a lot of code.




Theme © iAndrew 2016 - Forum software by © MyBB