Welcome Guest, Not a member yet? Register   Sign In
can i pass data from view to controller?
#21

[eluser]GrahamDj28[/eluser]
What does your getPosts function look like?

Maybe you can post it and i will check it out for you
#22

[eluser]brian88[/eluser]
Code:
function getPosts() {

  $q = $this->db->query("
   select p.id, body, p.date, p.userId, name, admin, picture
   from posts as p
   left join users as u on (p.userId = u.id)
   order by id desc
  ");
  if($q->num_rows() > 0){
   foreach($q->result() as $row){
    $data[] = $row;
   }
   return $data;
  }
} // end function
#23

[eluser]GrahamDj28[/eluser]
Hmmm, it should work.

It would help to see the complete code of main.php
#24

[eluser]GrahamDj28[/eluser]
I think I see what's going wrong

Change this:
Code:
$data['posts'][$key]['comments'] = $this->main_mod->getComments($post->id);

To:
Code:
$data['posts'][$key]->comments = $this->main_mod->getComments($post->id);

The elements in the array are objects, so you must asign the results as an object.
#25

[eluser]brian88[/eluser]
Thanks. That worked.

How do I output the first solution of yours?

Code:
var_dump($data['posts']); // outputs all the posts then another array under the post with comments
echo $data['posts']->comments; // error - Trying to get property of non-object

Adding in the 2nd soultion....
Now I get this error when using your getPosts function in model.
Message: Trying to get property of non-object
Filename: controllers/main.php
Line Number: 24

controller main
Code:
foreach($data['posts'] as $key => $post) {
    $data['posts'][$key]->comments = $this->main_mod->getComments($post->id); // line 24
   }

model
Code:
function getPosts() {
  $q = $this->db->query("
   select p.*, c.*
   from posts as p
   left join comments as c on (c.postId = p.id)
  ");

  if($q->num_rows() > 0) {
   $results = $q->result();
  }

  $posts = array();
  foreach($results as $post) {
   if( ! array_key_exists($post->id, $posts) ) {
    $posts[$post->id]['post'] = $post; //Collect the posts
   }
   $posts[$post->id]['comments'][]  = $post;
  }

  return $posts;
} // end function

function getComments($postId) {

  $comments = array();
  $this->db->where('postId', $postId);
  if( $q = $this->db->get('comments') ){
   $comments = $q->result();
  }
  return $comments;
} // end function
#26

[eluser]GrahamDj28[/eluser]
Can you post a var_dump() of $data['posts']; here.

And to be clear, you must use 1 of the solutions i provided. Not both.
#27

[eluser]brian88[/eluser]
I tried to space the code out so its readable.... theres 3 comments here and 2 posts.
Comments seemed to be attached to their posts. Not sure how to output it.
Code:
array(2) {
// post 1
[0]=> object(stdClass)#23 (8) { ["id"]=> string(2) "36" ["body"]=> string(17) "this is a post #2" ["date"]=> string(16) "Tuesday, May 1st" ["userId"]=> string(2) "32" ["name"]=> string(3) "joe" ["admin"]=> string(1) "0" ["picture"]=> string(9) "nopic.png"

["comments"]=> array(1) {
// comment 1
[0]=> object(stdClass)#25 (5) { ["id"]=> string(2) "63" ["comment"]=> string(14) "funny comment " ["postId"]=> string(2) "36" ["date"]=> string(19) "1:11pm Tue, May 1st" ["userId"]=> string(2) "34" } } }

// post 2
[1]=> object(stdClass)#24 (8) { ["id"]=> string(2) "35" ["body"]=> string(23) "this is an awesome post" ["date"]=> string(18) "Monday, April 30th" ["userId"]=> string(2) "31" ["name"]=> string(5) "chris" ["admin"]=> string(1) "0" ["picture"]=> string(6) "ok.jpg"

["comments"]=> array(2) {
// comment 1
[0]=> object(stdClass)#26 (5) { ["id"]=> string(2) "58"
["comment"]=> string(12) "cool comment" ["postId"]=> string(2) "35" ["date"]=> string(22) "8:57pm Mon, April 30th" ["userId"]=> string(2) "31" }
// comment 2
[1]=> object(stdClass)#27 (5) { ["id"]=> string(2) "64"
["comment"]=> string(5) "oh ok" ["postId"]=> string(2) "35" ["date"]=> string(19) "1:16pm Tue, May 1st" ["userId"]=> string(2) "34" } } } }
#28

[eluser]GrahamDj28[/eluser]
Well that's looking good.

To echo the comments you have to iterate through them like you do with the posts itself.

Code:
foreach($data['posts'] as $post) {
    //Echo the info about a post

    foreach($post->comments as $comment) {
        //Echo the info about a comment
    }

}

This is how you would display the posts and their comments no matter what solution of mine you are using.

But i have made some improvements!

I have updated my second solution for you. It can now filter out unwanted data from the results array. But for this you must return the results as an array containing array's and not objects.

Instead of returning
Code:
$query->result();

you must return
Code:
$query->result('array');

The function getPosts() should then look like this
Code:
function getPosts() {
    //Set the columns of the post you want in the final results
    $postColumns = array('column1','column2','column3');
    //Set the columns of the comment you want in the final results
    $commentColumns = array('column1','column2','column3','column4','column5');

    $q = $this->db->query("select p.*, c.*
                           from posts as p
                           left join comments as c on (c.postId = p.id)
                          ");
    if($q->num_rows() > 0) {
        $results = $q->result('array');
    }

    $posts = array();
    foreach($results as $post) {
        if( ! array_key_exists($post['id'], $posts) ) {
            $posts[$post['id']]['post'] = $this->filterColumns($post, $postColumns);
        }
        $posts[$post['id']]['comments'][]  = $this->filterColumns($post, $commentColumns);
    }

    return $posts;
} // end function

//If you use the above function, then you don't need this function
function getComments($postId) {
    $comments = array();
    $this->db->where('postId', $postId);
    if( $q = $this->db->get('comments') ){
        $comments = $q->result();
    }
    return $comments;
} // end function

//You will need this function
function filterColumns($row, $columns) {
    $result = array();
    foreach($row as $key => $value) {
        if( in_array($key, $columns) ) {
            $result[$key] = $value;
        }
    }
} // end function

When you var_dump() $posts at the end of the function it should look like this
Code:
array
  1 =>
    array
      'post' =>
        array
          'column1' => string '1' (length=1)
          'column2' => string 'auto' (length=4)
          'column3' => string 'Auto' (length=4)
      'comments' =>
        array
          0 =>
            array
              'column1' => string '1' (length=1)
              'column2' => string 'nadine-koster' (length=13)
              'column3' => string 'Nadine koster' (length=13)
              'column4' => string '2012-03-07 00:00:00' (length=19)
              'column5' => string 'Vlissingen' (length=10)
          1 =>
            array
              'column1' => string '2' (length=1)
              'column2' => string 'martin' (length=6)
              'column3' => string 'Martin' (length=6)
              'column4' => string '2012-03-22 00:00:00' (length=19)
              'column5' => string 'Middelburg' (length=10)

The method of iteration must now also be updated form
Code:
foreach($data['posts'] as $post) {
    //Echo the info about a post

    foreach($post->comments as $comment) {
        //Echo the info about a comment
    }

}

To:
Code:
foreach($data['posts'] as $post) {
    //Echo the info about a post

    foreach($post['comments'] as $comment) {
        //Echo the info about a comment
    }

}

To put it all together for you.

In your controller:
Code:
//Get al the posts and their comments
$data['posts'] = $this->main_mod->getPosts();

//Pass the $posts to the view
$this->load->view('home', $data);

In the view iterate the posts
Code:
foreach($data['posts'] as $post) {
    //Echo the info about a post

    foreach($post['comments'] as $comment) {
        //Echo the info about a comment
    }

}

That's it. Hope it works from the get go!
#29

[eluser]brian88[/eluser]
Thanks for the response.
You said the 1st solution is just running 1 extra query? Doesn't seem to be a big downfall. I 'm just going to use this solution. Seems to work fine and more simple.

I tried your updated version and I got a bunch of arrays with null for the post.

So we got the comments. But a comment is no good without the name of who posted it. Im trying to connect the comments with the correct user. Because right now, whoever created the post, their id, is always the id for the comment. So you have a post by tom and a bunch of comments by tom. But they are really comments from other people and not tom

controller
Code:
$data['posts'] = $this->main_mod->getPosts();

foreach($data['posts'] as $key => $p) {

    $data['posts'][$key]->comments = $this->main_mod->getComments($p->id, $p->userId);

    // this should be $comment->userId instead of $p->userId but i dont have that variable...
   }

   foreach($data['posts'] as $p){
    var_dump($p); // i have a link with a picture of the results below
   }
var_dump($p) results of 2 posts and 3 comments
http://dl.dropbox.com/u/9883217/work/db1.jpg
The "commentUserId" is the id i need...

model
Code:
function getPosts() {
  $q = $this->db->query("
   select p.id, body, p.date, p.userId, name, admin, picture
   from posts as p
   left join users as u on (p.userId = u.id)
   order by id desc
  ");
  if($q->num_rows() > 0){
   foreach($q->result() as $row){
    $data[] = $row;
   }
   return $data;
  }
} // end function

function getComments($postId, $userId) {
  $this->db->select('comments.postId, posts.userId as postUserId, comments.userId as commentUserId, comments.comment, users.name, users.picture, comments.date,');
  $this->db->join('posts', 'posts.id = comments.postId', 'left');

  // this should be comments.userId, not posts.userId but comments.userId returned nothing
  $this->db->join('users', 'users.id = posts.userId', 'left');

  $this->db->where('posts.id', $postId);
  $this->db->where('users.id', $userId);
  $q = $this->db->get('comments');
  
  if($q->num_rows() > 0){
   foreach($q->result() as $row){
    $data[] = $row;
   }
   return $data;
  }
} // end function

So my comments are always what the postId is. And it needs to be what the commentId is.
I feel like im really close.
#30

[eluser]GrahamDj28[/eluser]
Hi,

Glade to help.

Now to see if I understand correctly.

You have a table with users.
You have a table with posts which has the userId as reference column to user table.
You have a table with comments which has the userId as reference column to user table and postId as reference column to posts table.

Now are you sure that when a comment is added that the correct userId is being set? So the userId of the person placing the comment is set, and not the userId of the original post. No shame if this is the case. Easy mistake to make.

And for my updated solution, if the posts are NULL, you should also have comments as NULL. The query can't return comments if no posts are found. If that is the case the query you are running is not correct. Are you using the query i provided?

Maybe you can send me a sql dump of the tables in question.
I will then run some queries to see whats what.




Theme © iAndrew 2016 - Forum software by © MyBB