Chapter 6: Creating a Dashboard
164
Now that the controller and model have been updated, simply create the view with a form in it. The
following code snippet uses the Form helper functions extensively. First, include a header so you pull in
your title, and then open the form. Remember to post to admin/categories/create, as you ’ re checking for
POST data in that function. In other words, if there are no POST data, show this form. Otherwise, run the
addCategory() function you just added to the model.
< h1 > < ?php echo $title;? > < /h1 >
< ?php
echo form_open(‘admin/categories/create’);
Then it ’ s just a matter of adding form_input() fields for category name and shortdesc, and a
form_textarea for longdesc.
echo “ < p > < label for=’catname’ > Name < /label > < br/ > ”;
$data = array(‘name’= > ’name’,’id’= > ’catname’,’size’= > 25);
echo form_input($data) .” < /p > ”;
echo “ < p > < label for=’short’ > Short Description < /label > < br/ > ”;
$data = array(‘name’= > ’shortdesc’,’id’= > ’short’,’size’= > 40);
echo form_input($data) .” < /p > ”;
echo “ < p > < label for=’long’ > Long Description < /label > < br/ > ”;
$data = array(‘name’= > ’longdesc’,’id’= > ’long’,’rows’= > 5, ‘cols’= > ’40’);
echo form_textarea($data) .” < /p > ”;
For status, feed in an array that holds the two available statuses (active and inactive).
echo “ < p > < label for=’status’ > Status < /label > < br/ > ”;
$options = array(‘active’ = > ‘active’, ‘inactive’ = > ‘inactive’);
echo form_dropdown(‘status’,$options) .” < /p > ”;
For parentid, use the incoming $categories array as the possible choices in the dropdown.
echo “ < p > < label for=’parent’ > Category Parent < /label > < br/ > ”;
echo form_dropdown(‘parentid’,$categories) .” < /p > ”;
Finally, use form_submit() to add a Submit button, and close the form with form_close().
echo form_submit(‘submit’,’create category’);
echo form_close();
? >
Your Category Create form should look a lot like Figure 6 - 6.