can i pass data from view to controller? |
[eluser]gRoberts[/eluser]
Hi Brian, Apologies, I was meant to reply but got caught up in other work and unfortunately never got around to. The "Comments" column as it were, is not a column in your table, but one that is added by code within your controller. In my initial post, the code created an $Comments array against each Post that then contained all of the comments related to that post. I've corrected your code and inserted some comments. Code: <?
[eluser]brian88[/eluser]
Thanks a lot. I got the comments connected with their posts... However it is limiting only 1 comment per post. When I post a new comment it just replaces the old comment. But the old comments are still saved in the database. So the view is always showing the latest comment. I want to show all the comments for a post. I think the problem is in this logic. Code: //get all comments There are lots of comments in the database. But they are not showing up. It's only showing the newest comment for a post.
[eluser]gRoberts[/eluser]
If you do Code: var_dump($allComments) Is the `id` column the same for every comment? As a test, remove the `$c->id` from the line Code: $p->comments[$c->id] = $c; This should fix it until you find out why the wrong Comment ID is being returned against `$c->id`
[eluser]CroNiX[/eluser]
Each loop you are overwriting your array so it will only hold the last one in the loop. First, check to see if $p->comments is already set before creating it. Code: if ( ! isset($p->comments))
[eluser]gRoberts[/eluser]
[quote author="CroNiX" date="1335810778"]Each loop you are overwriting your array so it will only hold the last one in the loop. First, check to see if $p->comments is already set before creating it. Code: if ( ! isset($p->comments)) Unfortunately not my friend. If you look at the foreach statement that loops through the $posts or $data['posts'] variable, you'll notice that the $p->comments = array() is only set when looping through each post and not through each comment. As a result, $p->comments = array() will only ever be called once and never overwritten.
[eluser]brian88[/eluser]
Thanks a ton gRoberts! I got it to work. The id was not the same for every post. But I removed `$c->id` from the line and it worked. Are the reset() and unset() good practice? Because I took them out and it still worked fine.
[eluser]cPage[/eluser]
The question was : can i pass data from view to controller ? Code: $viewPlusData = $this->load->view('theview',$data,TRUE);
[eluser]brian88[/eluser]
Right cPage. But that returns the whole view's html. I wanted just an single id in that view. if theres someway to get that out that would be great. I have another question gRoberts. I got the comments to pop up. But I want to throw in the user name who posted it too. When I post then sign in as another user the comment says its from whoever created the post. So... when Chris makes a post. and David signs in and comments. The comment still says its Chris even though david is logged in and he posted it. controller Code: function index() { Code: // get all posts view Code: <?php foreach($posts as $p): ?> in my database the comments are correct with the right userIds. http://dl.dropbox.com/u/9883217/work/db.png when i Code: var_dump($allComments); http://dl.dropbox.com/u/9883217/work/comments.png
[eluser]GrahamDj28[/eluser]
Is it just me, or is this thread a little confusing? If I'm reading this right, it comes down to this. You have executed a query that gives you all the available posts, right? So why should you use a where in, if the post ids are already known and you have the function getComments? This is how it can be done Change the function getComments() to this: Code: public function getComments($postId) { You then call it like so: Code: foreach($posts as $key => $post) { Doing it this way does cause the system to execute an extra query for every post. So if you have 10 post results the total number of executed queries will be 11. That can cause some problems if the number of posts gets higher and higher. Solution: Change your getPosts() function so it will also get the comments with 1 query. Code: $query = "SELECT post.*, comment.* All the data you need will now be returned with this simple query As a complete function: Code: public function getPosts() { With both my solutions there will be a turning point where the one is better than the other. When the number of records gets very high, the second method could slow things down again. The database will sub query every result to find the comments, and a large data array can eat up memory and there is alot of overhead information being sent. So in some situations, more small queries is more friendly. And to anwser the original question: You can, by posting a form with the id in it. You will post the form with Javascript once the page is done loading. But you have multiple posts with multiple comments and posting forms. And you will generate alot of extra unwanted traffic. So you can, but you should not do it like that.
[eluser]brian88[/eluser]
Thanks for a response. Im trying your way also. I my first error is... Fatal error: Cannot use object of type stdClass as array in C:\Program Files (x86)\wamp\www\cms\application\controllers\main.php on line 26 Code: $data['posts'] = $this->main_mod->getPosts(); I added in your other code also. But got many errors. Maybe once we fix this one it will work better. |
Welcome Guest, Not a member yet? Register Sign In |