can i pass data from view to controller? |
[eluser]GrahamDj28[/eluser]
What does your getPosts function look like? Maybe you can post it and i will check it out for you
[eluser]brian88[/eluser]
Code: function getPosts() {
[eluser]GrahamDj28[/eluser]
Hmmm, it should work. It would help to see the complete code of main.php
[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.
[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 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) { model Code: function getPosts() {
[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.
[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) {
[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) { 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() { When you var_dump() $posts at the end of the function it should look like this Code: array The method of iteration must now also be updated form Code: foreach($data['posts'] as $post) { To: Code: foreach($data['posts'] as $post) { To put it all together for you. In your controller: Code: //Get al the posts and their comments In the view iterate the posts Code: foreach($data['posts'] as $post) { That's it. Hope it works from the get go!
[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(); http://dl.dropbox.com/u/9883217/work/db1.jpg The "commentUserId" is the id i need... model Code: function getPosts() { So my comments are always what the postId is. And it needs to be what the commentId is. I feel like im really close.
[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. |
Welcome Guest, Not a member yet? Register Sign In |