Welcome Guest, Not a member yet? Register   Sign In
Best way to populate an edit form from a database
#17

[eluser]satanuzo[/eluser]
PARA EDITAR

CONTROLLERS

Code:
public function edit_ciudad($id_ciudad){

        if ($this->session->flashdata('data_form')){
            $data['data_form']          = $this->session->flashdata('data_form');
        }else{
            $data['data_form']          = $this->localidad_model->ciudad_por_id($id_ciudad);
        }
        
        $data['opt_st_ciudad']      = $this->estado;
        $data['id_ciudad']          = $id_ciudad;
        $data['opt_region']         = $this->localidad_model->opt_region();

        $data['url_navegacion']     = anchor('localidad/list_ciudad/listar/'.$this->session->userdata('offset').'',$this->titulo_nav_2).' > '.'Actualizar ciudad';
        $data['url_accion']         = site_url('localidad/update_ciudad/'.$id_ciudad);
        // load view

        $this->load->view('head');
        $this->load->view('localidad_ciudad_edit', $data);
        $this->load->view('foot');
    }

    public function update_ciudad($id_ciudad){
        $this->session->set_userdata('id_ciudad', $id_ciudad);
        $data_form      = $this->input->post('data_form');

        $msg = '';

        $this->form_validation->set_rules('data_form[id_region]', 'Región', 'trim|required|xss_clean');
        $this->form_validation->set_rules('data_form[nombre_ciudad]', 'Nombre', 'trim|required|min_length[3]|xss_clean|callback_ciudad_check');
        
        $this->form_validation->set_rules('data_form[st_ciudad]', 'Estado', 'trim|required|xss_clean');
        // run validation
        if ($this->form_validation->run() == FALSE){
            $msg['error'] = 'No se pudo actualizar la ciudad, intentelo nuevamente.<br />'.validation_errors();
            $this->session->set_flashdata('msg', $msg);
            $this->session->set_flashdata('data_form', $data_form);
            redirect('localidad/edit_ciudad/'.$id_ciudad,'location', 301);
        }else{
            $num_filas_afectadas = $this->localidad_model->update_ciudad($id_ciudad, $data_form);
            $msg['ok'] = 'La ciudad ha sido actualizada.';
            $this->session->set_flashdata('msg', $msg);
            redirect('localidad/list_ciudad/listar/'.$this->session->userdata('offset').'','location', 301);
        }
    }

VIEW

Code:
<div class="content">
    <div class='tible'>&lt;?=$url_navegacion;?&gt;</div>
    &lt;?=form_open($url_accion)?&gt;
        <div class="data_form">
            <table>
                <tr>
                    <th width="30%">Id</th>
                    <td>&lt;?=form_input('data_form[id_region]', $id_region, 'disabled')?&gt;</td>
                </tr>
                <tr>
                    <th width="30%">Nombre<span class="requerido">&nbsp;*</span></th>
                    <td>&lt;?=form_input('data_form[nombre_region]', $data_form['nombre_region'])?&gt;</td>
                </tr>
                <tr>
                    <th>Estado<span class="requerido">&nbsp;*</span></th>
                    <td>&lt;?=form_dropdown('data_form[st_region]', $opt_st_region, $data_form['st_region'])?&gt;</td>
                </tr>
                <tr>
                    <th>&nbsp;</th>
                    <td>&lt;?=form_submit('actualizar','Actualizar')?&gt;</td>
                </tr>
            </table>
        </div>
    &lt;?=form_close()?&gt;
    <br />
</div>


la idea es que los campos del form se llamen igual a los campos de la db



CREATE TABLE IF NOT EXISTS `tbl_ciudad` (
`id_ciudad` smallint(3) unsigned NOT NULL AUTO_INCREMENT,
`nombre_ciudad` tinytext,
`id_region` tinyint(2) unsigned NOT NULL DEFAULT '0',
`st_ciudad` enum('activo','inactivo') NOT NULL DEFAULT 'activo',
PRIMARY KEY (`id_ciudad`),
KEY `id_region` (`id_region`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT AUTO_INCREMENT=349 ;

en el formulario
&lt;input type="test" name="data_form[nombre_ciudad]"

MODELS

Code:
function insert_ciudad($data_form)
    {
        $this-&gt;db->insert('tbl_ciudad', $data_form);
        return $this->db->insert_id();
    }

    function update_ciudad($id_ciudad, $data_form)
    {
        $this->db->update('tbl_ciudad', $data_form, array('id_ciudad' => $id_ciudad));
        return $this->db->affected_rows();
    }

    function ciudad_por_id($id_ciudad)
    {
        $sql="
            SELECT
                    tbl_ciudad.id_ciudad,
                    tbl_ciudad.nombre_ciudad,
                    tbl_region.nombre_region,
                    tbl_region.id_region,
                    tbl_ciudad.st_ciudad

            FROM    tbl_ciudad, tbl_region
            WHERE   tbl_ciudad.id_ciudad = $id_ciudad
            AND     tbl_ciudad.id_region = tbl_region.id_region";
        $result = $this->db->query($sql);
        return $result->row_array();
    }


Messages In This Thread
Best way to populate an edit form from a database - by El Forum - 04-30-2009, 09:19 AM
Best way to populate an edit form from a database - by El Forum - 04-30-2009, 10:14 AM
Best way to populate an edit form from a database - by El Forum - 04-30-2009, 10:25 AM
Best way to populate an edit form from a database - by El Forum - 05-01-2009, 10:01 AM
Best way to populate an edit form from a database - by El Forum - 05-22-2009, 10:15 AM
Best way to populate an edit form from a database - by El Forum - 05-24-2009, 02:02 AM
Best way to populate an edit form from a database - by El Forum - 05-24-2009, 07:19 PM
Best way to populate an edit form from a database - by El Forum - 01-05-2010, 07:10 PM
Best way to populate an edit form from a database - by El Forum - 01-05-2010, 07:33 PM
Best way to populate an edit form from a database - by El Forum - 01-06-2010, 07:02 AM
Best way to populate an edit form from a database - by El Forum - 01-08-2010, 08:20 AM
Best way to populate an edit form from a database - by El Forum - 02-15-2010, 07:06 AM
Best way to populate an edit form from a database - by El Forum - 03-31-2010, 05:58 AM
Best way to populate an edit form from a database - by El Forum - 03-31-2010, 09:03 AM
Best way to populate an edit form from a database - by El Forum - 03-23-2011, 11:33 AM
Best way to populate an edit form from a database - by El Forum - 07-06-2011, 01:40 PM
Best way to populate an edit form from a database - by El Forum - 07-06-2011, 01:45 PM
Best way to populate an edit form from a database - by El Forum - 07-06-2011, 02:57 PM



Theme © iAndrew 2016 - Forum software by © MyBB