![]() |
Form used in new and edit mode - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Form used in new and edit mode (/showthread.php?tid=22912) |
Form used in new and edit mode - El Forum - 09-23-2009 [eluser]Samuurai[/eluser] Hi everyone, Just wondering if I am doing this in a really stupid way or not, can someone please tell me if this is the best way to use a form for both registering and for editing the users details later? I load the view with the $new or $edit variable set, depending on what mode I want to be in. If not variable is set, the form loads in view mode onle. Then in the view, my code looks like this: Code: <?=if(isset($new)):?> Form used in new and edit mode - El Forum - 09-23-2009 [eluser]bretticus[/eluser] Don't pass your row to the view. Instead create variables in the controller (like $first_name) and set them to an empty string when not in edit mode. You can still use one view without having 40 if else then blocks in it ![]() Form used in new and edit mode - El Forum - 09-23-2009 [eluser]Samuurai[/eluser] Great! That's good advice! Should I make a new view for displaying the data? Form used in new and edit mode - El Forum - 09-23-2009 [eluser]bretticus[/eluser] [quote author="Samuurai" date="1253748597"] Should I make a new view for displaying the data?[/quote] Yes, since you'd end up changing the view entirely, you should just have another view. Of course it really wouldn't have hurt to have had a duplicate view of your form in the first place. Form used in new and edit mode - El Forum - 09-23-2009 [eluser]Samuurai[/eluser] Ok, brilliant.. thanks for that! Form used in new and edit mode - El Forum - 09-24-2009 [eluser]Samuurai[/eluser] I think i'm having a bit of a dull moment, but I was looking at my code and wondering if I do really need a foreach when displaying the query results. My model does the query and returns $query->result(); I then pass that to my view as $data->result Then in my view I do a foreach($result as $row) Then I access the data using $row->firstname or whatever. I want to pass the data to my views from the controller.. I've tried various differnt combinations of $result->row()->firstname, but I keep getting errors like "Call to member function on a non-object. Do I really need the foreach? Form used in new and edit mode - El Forum - 09-24-2009 [eluser]wabu[/eluser] Hello, if this is for a single row, you can do the following in your model: Code: return $query->row(); Form used in new and edit mode - El Forum - 09-24-2009 [eluser]Samuurai[/eluser] Ah right... I think I've got it now. I then did $row = $result[0] And I can access everything using $row->firstname! Woohoo! thanks everyone for your help! |