![]() |
Combine Add / Edit views - 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: Combine Add / Edit views (/showthread.php?tid=36586) |
Combine Add / Edit views - El Forum - 12-07-2010 [eluser]atebit[/eluser] I am attempting to combine my add and edit views and have some questions regarding best approach. (The wiki has some info, but is for older version of CI). I am currently using 1.7.3. Basic setup... when I call edit, I pass a $data array to populate the fields: Code: <?=form_input('email', $user->email)?> Obviously with add, $data will not exist. Code: <?=form_input('email', set_value('email'))?> The quick and dirty (wrong) way would be... Code: <? if(isset($user)) ?> Attempting to keep up with DRY philosophy, what is best approach? Combine Add / Edit views - El Forum - 12-07-2010 [eluser]Kindari[/eluser] You could do $edit = true / false. Also the second parameter of set_value() allows a default value. Code: <?=form_input('email', set_value('email', $edit ? $user->email : '' ) )?> Now some people simply do Code: <?=form_input('email', set_value('email', $user->email ) )?> But this would generate warnings/errors (depending on your server) if $user is not defined. I prefer to be explicit. Combine Add / Edit views - El Forum - 12-08-2010 [eluser]helmutbjorg[/eluser] That is how I would do it except you don't need the $edit variable at all. If a user exists then we know we are editing. Code: <?=form_input('email', set_value('email', isset($user) ? $user->email : '' ) )?> Combine Add / Edit views - El Forum - 12-09-2010 [eluser]atebit[/eluser] Thanks for the suggestion guys!! Both are good approaches. Combine Add / Edit views - El Forum - 12-09-2010 [eluser]cityzen[/eluser] scratch my post... not very DRY looking back on it... |