CodeIgniter Forums

Full Version: Combine Edit & Input Views
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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>

El Forum

[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;

El Forum

[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.

El Forum

[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.

El Forum

[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]

El Forum

[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.