Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Seeking advice on trimming amount of code for a CRUD form
#1

[eluser]Nicholai[/eluser]
I'm starting to learn the ropes, but I feel like I'm making things harder than they need to be. I am creating a simple form that uses the same view for both adding and updating. When updating it populates the fields from the database, and when adding it presents blank fields. So far, I've found that I have to duplicate the form in order to make this work, like so:
Code:
<?php if ($query != ''): ?>
<?php foreach($query as $row):  ?>

<p>
&lt;?php echo form_open('tasktypes/insert'); ?&gt;
&lt;input type="hidden" value="&lt;?php echo $ID; ?&gt;" name="ID" /&gt;
<table>
<tr><td>Name: </td><td>&lt;input type="text" name="ttName" value="&lt;?php echo $row-&gt;ttName; ?&gt;"/></td></tr>
<tr><td>Description: </td><td>&lt;input type="text" name="ttDescription" value="&lt;?php echo $row-&gt;ttDescription; ?&gt;"/></td></tr>
<tr><td>Priority: </td><td>&lt;input type="text" name="ttPriority" value="&lt;?php echo $row-&gt;ttPriority; ?&gt;"/></td></tr>
<tr><td>Display Order: </td><td>&lt;input type="text" name="ttDisplayOrder" value="&lt;?php echo $row-&gt;ttDisplayOrder; ?&gt;"/></td></tr>
<tr><td>&lt;input type="image" src="http://nicholaibu-d630:8080/CodeIgniter/images/edit.png" value="Add" /&gt;&lt;/td></tr>
&lt;/form&gt;
</p>
&lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;


&lt;?php if (!$ID): ?&gt;
&lt;?php echo form_open('tasktypes/insert'); ?&gt;
<table>
<tr><td>Name: </td><td>&lt;input type="text" name="ttName" value=""/&gt;&lt;/td></tr>
<tr><td>Description: </td><td>&lt;input type="text" name="ttDescription" value=""/&gt;&lt;/td></tr>
<tr><td>Priority: </td><td>&lt;input type="text" name="ttPriority" value=""/&gt;&lt;/td></tr>
<tr><td>Display Order: </td><td>&lt;input type="text" name="ttDisplayOrder" value=""/&gt;&lt;/td></tr>
<tr><td>&lt;input type="image" src="http://nicholaibu-d630:8080/CodeIgniter/images/edit.png" value="Add" /&gt;&lt;/td></tr>
&lt;/form&gt;
</p>
&lt;?php endif; ?&gt;

My model checks if an ID is being passed in; if yes, it updates the record and if no, it adds a new record. My controller is pretty simple:

Code:
function add_update()
    {
        $data['title'] = "Task Types";
        
        $tasktypeID = $this->uri->segment(3);
        $data['ID'] = $tasktypeID;

        $data['query'] = $this->Admin_tasks_model->get_task_types($tasktypeID);

        
        $this->load->view('admin_menu_view.php', $data);
        $this->load->view('admin_add_new_task_type_view.php', $data);
    }

    function insert()
    {
    
        $this->Admin_tasks_model->ID = ($_POST['ID']);
        $this->Admin_tasks_model->save($_POST);
        redirect('tasktypes/display');
    }

Is there an easier way to reuse the form? It seems like the way I've set it up, form validation won't play nicely either. Should I split add and edit into two views (still using the same controller and model?)

I've been doing lots of reading and testing (and failing), trying to understand the "right way" to do things. Thanks for any advice.


Messages In This Thread
[SOLVED] Seeking advice on trimming amount of code for a CRUD form - by El Forum - 04-24-2009, 07:17 PM



Theme © iAndrew 2016 - Forum software by © MyBB