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

[eluser]brian88[/eluser]
Yes the userId is correct when a user comments. In my picture I posted of the dump, the commentUserId is the correct user as it differs from the postUserId(which is being used instead).

Im puzzled at how to get the correct userId for comments.

Here is the sql dump
http://dl.dropbox.com/u/9883217/work/posts.zip

and yes the post and comments is null. and im selecting everything from posts like u did and didnt change your code.
#32

[eluser]GrahamDj28[/eluser]
That is weird.

I will use your dump and get back to you in a moment.
#33

[eluser]GrahamDj28[/eluser]
Hi,

you only provided the post table. I will need the user and comments table as well.
#34

[eluser]GrahamDj28[/eluser]
Ok,

I have done some testing and here are me results.

I created the user and comment tables for the testing. The columns will not match your own, but it comes down to the same thing.


USER TABLE
id | username | name
------------------------------------
31 | GrahamDj | Martin Langenberg
------------------------------------
32 | Brian88 | Brian Forum User
------------------------------------
33 | RandomUser | Some Name Here
------------------------------------
34 | SomeUserName | This Is His Name


POSTS TABLE
id | body | date | userId
---------------------------------------------------
35 | this is a post | Monday, April 30th | 31
---------------------------------------------------
36 | yaaaa | Tuesday, May 1st | 32


COMMENT TABLE
id | userId | postId | date | comment
----------------------------------------------------------------------------------------
1 | 33 | 35 | 2012-05-02 22:46:55 | This is a response to post id 35
----------------------------------------------------------------------------------------
2 | 34 | 35 | 2012-05-02 22:46:55 | This is a second response to post id 35
----------------------------------------------------------------------------------------
3 | 33 | 36 | 2012-05-02 22:46:55 | This is a response to post id 36


THE QUERY
Code:
SELECT p.*, c.*,
up.username AS ownerOfPost,
uc.username AS ownerOfComment
FROM posts AS p
LEFT JOIN comment AS c ON c.postId = p.id
LEFT JOIN user AS up ON up.id = p.userId
LEFT JOIN user AS uc ON uc.id = c.userId

THE RESULT OF THE QUERY
Link to image

Before changing your code, update the query to match your table and column names and then run the query in phpmyadmin.

Good luck!
#35

[eluser]brian88[/eluser]
ahh im sorry. heres the full dump...
http://dl.dropbox.com/u/9883217/blog.sql

i will test your code tomorrow. thanks a ton!
#36

[eluser]brian88[/eluser]
Thanks GrahamDj28 and gRoberts for your help.

Just to sum up this long post. We had 2 ways of linking comments to posts.

this was the simplest way and works fine.

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

// goes into the posts
foreach($data['posts'] as $key => $p) {
// and links all the comments with their correct postId.
    $data['posts'][$key]->comments = $this->main_mod->getComments($p->id);
   }
// all posts have their comments now.

// dump out all the results

foreach($data['posts'] as $p) { // goes into all the posts
    var_dump($p); // returns all posts and a comments array

    foreach($p->comments as $c) { // goes into the comments array
     var_dump($c); // returns all comments for that post
    }

   }

model
Code:
// get all posts
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 p.id DESC
  ");

  if($q->num_rows() > 0){
   foreach($q->result() as $row){
    $data[] = $row;
   }
   return $data;
  }
} // end function

// get all comments
function getComments($postId) {
  $q = $this->db->query("
   SELECT c.date, c.comment, uc.picture, uc.name AS ownerOfComment
   FROM posts AS p
   LEFT JOIN comments AS c ON c.postId = p.id
   LEFT JOIN users AS up ON up.id = p.userId
   LEFT JOIN users AS uc ON uc.id = c.userId
   WHERE p.id = ?
   ORDER BY uc.id DESC
  ", array($postId));

  if($q->num_rows() > 0){
   foreach($q->result() as $row){
    $data[] = $row;
   }
   return $data;
  }
} // end function
#37

[eluser]GrahamDj28[/eluser]
Hi,

Glade to here it's all working now.

If you use this construction, then you can change the comments query to this:
Code:
SELECT c.date, c.comment,
u.picture,
u.name AS ownerOfComment
FROM comments AS c
LEFT JOIN users AS u ON u.id = c.userId
WHERE c.postId = ?
ORDER BY c.date ASC

As the query is now, you can select posts and their comments all at once.
#38

[eluser]brian88[/eluser]
Im glad you posted that sql. I just ran into a problem. When posts do not have a comment yet. It just returns all null for all the table data columns. So when you create a new post, It automatically has a null comment to start with.

Now it returns the whole array as null. and no more null comment.

This sql fixed it. Thanks!
#39

[eluser]GrahamDj28[/eluser]
You are welcome!

Good luck with the rest!

PS. If you use CI's profile you can see the number of queries being executed during a page call.




Theme © iAndrew 2016 - Forum software by © MyBB