CodeIgniter Forums
Combine Edit & Input 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 Edit & Input Views (/showthread.php?tid=5063)



Combine Edit & Input Views - El Forum - 01-03-2008

[eluser]scottelundgren[/eluser]
Is there an elegant way for a view to be used for both data entry of a new record and editing an existing record? I'm trying to add the following logic. If you see a good way to combine these samples of 2 views into 1 view and retain validation I'd appreciate it. Thanks!

look up student record
if a record is found, load the data from the row into $data, pass $data into the view:
Code:
<tr>
  <td width="85%">I take attendance </td>
  <td width="15%" align="center" valign="middle">&lt;?=form_dropdown('attendance',$options, $attendance)?&gt;</td>
</tr>
<tr>
<td>Number of Absences </td>
<td align="center" valign="middle">&lt;input name="absences" type="text" size="3" maxlength="3" value="&lt;?=$absences?&gt;"/&gt;</td>
</tr>
if a record is not found, just load the view:
Code:
<tr>
  <td width="85%">I take attendance </td>
  <td width="15%" align="center" valign="middle">&lt;?=form_dropdown('attendance',$options, $this->validation->attendance)?&gt;</td>
  </tr>
  <tr>
  <td>Number of Absences </td>
  <td align="center" valign="middle">&lt;input name="absences" type="text" size="3" maxlength="3" value="&lt;?=$this-&gt;validation->absences?&gt;"/></td>
</tr>



Combine Edit & Input Views - El Forum - 01-03-2008

[eluser]Pygon[/eluser]
you could embed an if statement, such as:

Code:
&lt;td align="center" valign="middle"&gt;
&lt;input name="absences"
type="text"
size="3"
maxlength="3"
value="&lt;?=(empty($absences)?$this-&gt;validation->absences:$absences)?&gt;"
/&gt;&lt;/td&gt;



Combine Edit & Input Views - El Forum - 01-03-2008

[eluser]Negligence[/eluser]
I set a mode flag (to create or edit) and determine what to show/hide depending on the flag. Same goes for validation.


Combine Edit & Input Views - El Forum - 01-03-2008

[eluser]Craig A Rodway[/eluser]
This is something I have used since I started with CI and subsequently wrote this article in the wiki. Hope it helps.


Combine Edit & Input Views - El Forum - 01-03-2008

[eluser]scottelundgren[/eluser]
Ah! That should do it. Thanks!

[quote author="Pygon" date="1199397555"]you could embed an if statement, such as:
Code:
&lt;td align="center" valign="middle"&gt;
&lt;input name="absences"
type="text"
size="3"
maxlength="3"
value="&lt;?=(empty($absences)?$this-&gt;validation->absences:$absences)?&gt;"
/&gt;&lt;/td&gt;
[/quote]


Combine Edit & Input Views - El Forum - 01-03-2008

[eluser]scottelundgren[/eluser]
[quote author="Craig Rodway" date="1199404269"]This is something I have used since I started with CI and subsequently wrote this article in the wiki. Hope it helps.[/quote]

That article was helpful. Unfortunately my app is a series of post forms because I have to carry multiple variables so I can't use the uri segment code like the example but I get the gist of the fields function that encapsulates the suggestion of Pygon.