[eluser]Alejandrito[/eluser]
I´m new to code igniter so I hope you can help me with this.
I have a form to edit an article so I get the data from the DB and set the values of that article so the form is not empty. My question is: how do I use the new/old info to update the DB?
my Controller looks like this:
Code: function confirm_edit($id)
{
$this->form_validation->set_rules('nombre', 'Nombre', 'trim|xss_clean');
$this->form_validation->set_rules('codigo', 'Código', 'trim|is_numeric');
$this->form_validation->set_rules('cod_barras', 'Código de Barras', 'trim');
$this->form_validation->set_rules('descripcion', 'Descripción', 'trim');
$this->form_validation->set_rules('familia', 'Familia', 'trim');
$this->form_validation->set_rules('subfamilia', 'Subfamilia', 'trim');
$this->form_validation->set_rules('proveedores', 'Proveedores', 'trim');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
/* NOT SURE ABOUT THIS PART!! */
$datos = array(
'nombre' => set_value('nombre'),
'codigo' => set_value('codigo'),
'cod_barras' => set_value('cod_barras'),
'descripcion' => set_value('descripcion'),
'familia' => set_value('familia'),
'subfamilia' => set_value('subfamilia'),
'proveedor' => set_value('proveedor')
);
if ($datos['articulos']=$this->mod_articulos->editar($id))
{
echo "ok";
}
}
This is the function I use in the model:
Code: function editar($id)
{
$datos = $this->mod_articulos->unArticulo($id);
$editar = array(
"familia" => $datos[0]->familia,
'subfamilia' => $datos[0]->subfamilia,
'codigo' => $datos[0]->codigo,
'cod_barras' => $datos[0]->cod_barras,
'nombre' => $datos[0]->nombre,
'descripcion' => $datos[0]->descripcion,
'pcp' => $datos[0]->pcp,
'pvp' => $datos[0]->pvp,
'proveedor' => $datos[0]->proveedor,
'stock' => $datos[0]->stock,
'minstock' => $datos[0]->minstock
);
$this->db->where('id', $datos[0]->id);
$this->db->update('articulos', $editar);
}
the view looks like this:
Code: <tr>
<td><label for="descripcion">Descripción</label></td>
<td><input id="descripcion" type="text" name="descripcion" value="<?php echo set_value('descripcion', $articulo[0]->descripcion); ?>" > <br><?php echo form_error('descripcion'); ?></td>
</tr>
but despite everything, the query doesn't reflect the changes made in the DB. I`m always changing the "descripcion" field but this is what the debugger says:
Code: UPDATE `articulos` SET `familia` = '02', `subfamilia` = '01', `codigo` = '02', `cod_barras` = '13123', `nombre` = 'Guantes de plastico', `descripcion` = '', `pcp` = '5.00', `pvp` = '9.00', `proveedor` = '02', `stock` = '100', `minstock` = '10' WHERE `id` = '4'
(the item with id=4 exists)
I would be very thankful if any of you can give me a hint on this matter!!
[eluser]tente[/eluser]
How do you handle to get the $id of the db table entry?
And what is this doing?:
Code: $datos = $this->mod_articulos->unArticulo($id);
Maybe you're charging the previous database data?
What I'd do is setting a hidden input with the id and trying to read it when the form is posted. And then, sell all the data to the model, if the id is false, create a new entry, if not: update.
[eluser]Alejandrito[/eluser]
Hi!
unArticulo($id) gets the info of one item only. I use this to show a single item
When I see a single item, I have an "edit" button, like this:
Code: <td class="center"><a href="<?php echo site_url('articulos/editar/'.$articulo[0]->id)?>">Editar</a></td>
This would be the editing view's body:
Code: <body>
<?php echo form_open('articulos/confirm_edit/'.$articulo[0]->id); ?>
<table class="center">
<thead>
<tr><th colspan="2" class="center">Editar "<?=$articulo[0]->nombre;?>"</th></tr>
</thead>
<tbody>
<tr>
<td><label for="nombre">Nombre</label></td>
<td><input id="nombre" type="text" name="nombre" value="<?=$articulo[0]->nombre?>"><br><?=form_error('nombre')?></td>
</tr>
<tr>
<td><label for="codigo">Código</label></td>
<td><input id="codigo" type="text" name="codigo" value="<?php echo $articulo[0]->familia; echo $articulo[0]->subfamilia; echo $articulo[0]->codigo?>" ><br> <?php echo form_error('codigo'); ?></td>
</tr>
<tr>
<td><label for="cod_barras">Código de Barras</label></td>
<td><input id="cod_barras" type="text" name="cod_barras" value="<?php echo set_value('cod_barras', $articulo[0]->cod_barras); ?>" ><br> <?php echo form_error('cod_barras'); ?></td>
</tr>
<tr>
<td><label for="descripcion">Descripción</label></td>
<td><input id="descripcion" type="text" name="descripcion" value="<?php echo set_value('descripcion', $articulo[0]->descripcion); ?>" > <br><?php echo form_error('descripcion'); ?></td>
</tr>
<tr>
<td><label for="proveedor">Proveedor</label></td>
<td><?php echo form_dropdown('proveedor', array('' => 'Proveedor'));?><br><?php echo form_error('proveedor'); ?></td>
</tr>
<tr>
<td><label for="familia">Familias</label></td>
<td><?php echo form_dropdown('familia', array('' => 'Familia'));?><br><?php echo form_error('familia'); ?>
</td>
</tr>
<tr>
<td><label for="pvp">Precio de Venta</label></td>
<td><input id="pvp" type="text" name="pvp" value="<?php echo set_value('pvp', $articulo[0]->pvp); ?>" ></td>
</tr>
<tr>
<td><label for="pcp">Precio de Compra</label></td>
<td><input id="pcp" type="text" name="pcp" value="<?php echo set_value('pcp', $articulo[0]->pcp); ?>" ></td>
</tr>
<tr>
<td><label for="subfamilia">Subfamilia</label></td>
<td><?php echo form_dropdown('subfamilia', array('' => 'Subfamilia')); ?><br><?php echo form_error('subfamilia'); ?></td>
</tr>
<tr>
<td colspan="2" class="center"><?php echo form_submit( 'submit', 'Enviar');?> </td>
</tr>
</tbody>
</table>
<?php echo form_close(); ?>
</body>
Oh! and I'm sure is note false
[eluser]weboap[/eluser]
read the data for that specific $id
something like
in model
Code: public function getByID( $id ) {
$id =(int)$id;
$query = $this->db->where( 'id', $id )->limit( 1 )->get( 'articulos' );
if( $query->num_rows() > 0 ) {
return $query->row();
} else {
return false;
}
}
in controller
method to show data only no validation
Code: function edit($id){
$data['records'] = $this->your_model->getByID($id);
$this->load->view('my_fom', $data);
}
in the form view
Code: <?php if(!$records){
echo "unavailable data!";
else{
?>
.....
//INPORTANT use a hidden input for ID
//your form action to go to validation method example : site_url('yourcontroller/confirm_edit');
//your form with set_value( $records->field_name ) for each input
.....
<? }?>
in the confirm_edit
Code: function confirm_edit()
{
$this->form_validation->set_rules('nombre', 'Nombre', 'trim|xss_clean');
$this->form_validation->set_rules('codigo', 'Código', 'trim|is_numeric');
$this->form_validation->set_rules('cod_barras', 'Código de Barras', 'trim');
$this->form_validation->set_rules('descripcion', 'Descripción', 'trim');
$this->form_validation->set_rules('familia', 'Familia', 'trim');
$this->form_validation->set_rules('subfamilia', 'Subfamilia', 'trim');
$this->form_validation->set_rules('proveedores', 'Proveedores', 'trim');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('my_form');
}
else
{
$data = array(
'id'=>$this->input->post('id'),
'nombre'=>$this->input->post('nombre'),
'code'=>$this->input->post('code'),
.........
);
$updated=$this->mod_articulos->editar($data)
if ($updated)
{
echo "ok";
}else{
echo "error";
}
}
in your model again now to save the data
Code: function editar($data){
$this->db->where('id', $data['id']);
$updated = $this->db->update('articulos', $data);
return $updated;
}
the above need some correction of names and more work but you get the logic!
|