Welcome Guest, Not a member yet? Register   Sign In
Insert and update in the same view
#1

[eluser]il_dandi[/eluser]
I've a control that manage Add and Update fuctions and that runs add_entry() and update_entry() of model.

Is it possible to create a view (a single page) that manages insert and update both?? Without create two differents page??

Can you tell me the best and secure solution for manage this?


Thanks!
#2

[eluser]pistolPete[/eluser]
Have a look at Add_Edit_Views.
#3

[eluser]il_dandi[/eluser]
Hi pistolPete, I've seen the example! I would like to apply this solution to my page.... I've made many test but I've a probem. I hope that you can help me

controller
Code:
function add()
    {    
        $this->load->helper('form');
                        
        if ( ! $this->_validate_form() )
        {
            $this->template->write_view('content', 'admin/avatar', $data, TRUE);
            $this->template->render();
        }
        else
        {
            $id = $this->avatars_model->add_entry();
            $this->session->set_flashdata('msg', 'OK');
            redirect(base_url()."admin/avatars");
        }                
    }

model
Code:
function add_entry()
    {        
        $fields = array(
                    'FK_category_id'    =>    $category,
                    'title'                =>    $this->input->post('title'),
                    'description'        =>    $this->input->post('description'),
                    'url'                =>    $this->input->post('url')
                        );
        
        
        $this->db->set($fields);
        $this->db->insert('avatars');
        $id = $this->db->insert_id();
        return $id;    
        
    }
    

    function update_entry($id)
    {    
        $fields = array(
                    'FK_category_id'    =>    $category,
                    'title'                =>    $this->input->post('title'),
                    'description'        =>    $this->input->post('description'),
                    'url'                =>    $this->input->post('url')
                        );
        
        $this->db->set($fields);
        $this->db->where('id', $id);
        $this->db->update($this->entries_table);
    }


Can you tell me how to modify the view for manage update and insert in the same page?

Thanks
#4

[eluser]jedd[/eluser]
EDIT - mispost, sorry.
#5

[eluser]il_dandi[/eluser]
Nobody can help me?? :-((
#6

[eluser]pistolPete[/eluser]
[quote author="il_dandi" date="1237168650"]Can you tell me how to modify the view for manage update and insert in the same page?
[/quote]

You didn't post your view file, so I can't "modify" it.
If you had really tried the example from the wiki, you'd know that you need 3 functions in your controller: add(), edit(), save()
In another thread I saw you are still using the old Validation class instead of the newer Form validation library; I'd recommend the latter because it's easier to repopulate the forms.
#7

[eluser]il_dandi[/eluser]
Do you mean that the validation that I'm using in my last post is old??

I have to use the validation that I found in the wiki of this thread?

I've seen the example!!

What I don't know is how to manage the view!! I the post I see <fieldset> ...

in my view I've the input, combo, check... fields and not the fieldset! How Can I use my view with the example that you have posted?

Thanks thanks thanks!!
#8

[eluser]il_dandi[/eluser]
In my view file I've


Code:
&lt;?=form_open("admin/avatars/add");?&gt;&lt;?=form_hidden('id', $id)?&gt;

<table width="100%">
<tr>
<td width="100px" align="right">nome: </td>
<td>&lt;input name="firstname" type="text" id="firstname" size="30" maxlength="255" value="&lt;?=$firstname?&gt;"&gt;&lt;/td>
</tr>
<tr>
  <td align="right">cognome: </td><td>&lt;input name="lastname" type="text" id="lastname" size="30" maxlength="255" value="&lt;?=$lastname?&gt;" class="&lt;?=$v-&gt;lastname_error?&gt;" ></td>
  </tr>
<tr>
  <td align="right">age</td>
  <td>&lt;?=$age?&gt;</td>
  </tr>
<tr>
  <td align="right">contry: </td><td>
<select name="idCountry">
&lt;? foreach ($countries->result() as $row):?&gt;
    <option value="&lt;?=$row->id?&gt;" &lt;? if ($idCountry==$row->id) echo " selected";?&gt;>
    &lt;?=$row->description?&gt;
    </option>
&lt;? endforeach?&gt;
</select>
  </td>
  </tr>
</table>

       &lt;/form&gt;


Can you help me to create the 3 functions for manage update and insert?
#9

[eluser]pistolPete[/eluser]
Quote:Do you mean that the validation that I’m using in my last post is old??

Yes.
From the user guide:
Quote:Note: As of CodeIgniter 1.7.0, this Form Validation class supercedes the old Validation class, which is now deprecated.

I'd suggest rewriting your code to work with the new class.
After that , have a look at this thread: http://ellislab.com/forums/viewreply/533725/

Quote:I have to use the validation that I found in the wiki of this thread?
The example from the wiki is a bit outdated, I might update it sometimes.

Quote:I the post I see <fieldset> ...
Have a look at w3.org if you don't know some HTML elements...
#10

[eluser]il_dandi[/eluser]
In my view file I’ve

Code:
&lt;?=form_open("admin/avatars/add");?&gt;&lt;?=form_hidden('id', $id)?&gt;

<table width="100%">
<tr>
<td width="100px" align="right">nome: </td>
<td>&lt;input name="firstname" type="text" id="firstname" size="30" maxlength="255" value="&lt;?=$firstname?&gt;"&gt;&lt;/td>
</tr>
<tr>
  <td align="right">cognome: </td><td>&lt;input name="lastname" type="text" id="lastname" size="30" maxlength="255" value="&lt;?=$lastname?&gt;" class="&lt;?=$v-&gt;lastname_error?&gt;" ></td>
  </tr>
<tr>
  <td align="right">age</td>
  <td>&lt;?=$age?&gt;</td>
  </tr>
<tr>
  <td align="right">contry: </td><td>
<select name="idCountry">
&lt;? foreach ($countries->result() as $row):?&gt;
    <option value="&lt;?=$row->id?&gt;" &lt;? if ($idCountry==$row->id) echo " selected";?&gt;>
    &lt;?=$row->description?&gt;
    </option>
&lt;? endforeach?&gt;
</select>
  </td>
  </tr>
</table>

       &lt;/form&gt;

Can you help me to create the 3 functions for manage update and insert?




Theme © iAndrew 2016 - Forum software by © MyBB