Can not display content from database for editing in the form with CodeIgniter 3

时间:2021-01-23 23:06:35

I am trying for some time to display the values of each column in the fields of the selected row in my editing form. The update operation is updating normally, I just need to display it otherwise I either have to write everything all over again or save in blank.

我正在尝试在编辑表单中显示所选行的字段中每列的值。更新操作正常更新,我只需要显示它,否则我要么必须重新编写所有内容或保存为空白。

I have my own model called MY_Model.php:

我有自己的模型MY_Model.php:

<?php

class MY_Model extends CI_Model
{
    public $table = '';
    protected $primary_key = 'id';

    public function __construct()
    {
        parent::__construct();

        $this->load->database();
        $this->load->helper('inflector');
    }

    public function get($id)
    {
        return $this->db->get_where($this->table, array($this->primary_key = $id))->row();
    }

    public function select()
    {
        $data = array();

        $this->db->from($this->table);
        $this->db->order_by($this->primary_key, 'DESC');
        $query = $this->db->get();

        foreach ($query->result_array() as $row) {
            $data[] = $row;
        }

        $query->free_result();

        return $data;
    }

    public function insert($data)
    {
        $data['date_created'] = $data['date_updated'] = date('Y-m-d');
        $data['created_from_ip'] = $data['updated_from_ip'] = $this->input->ip_address();

        if ($this->db->insert($this->table, $data)) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    public function update($data, $id)
    {
        $data['date_updated'] = date('Y-m-d');
        $data['updated_from_ip'] = $this->input->ip_address();

        $this->db->where($this->primary_key, $id);

        return $this->db->update($this->table, $data);
    }

    public function delete($id)
    {
        $this->db->where($this->primary_key, $id);

        return $this->db->delete($this->table);
    }

    public function count_all()
    {
        return $this->db->count_all($this->table);
    }
}

Then, the edit method of my Customers controller:

然后,我的客户控制器的编辑方法:

public function edit($id)
{
    if ($this->input->post()) {
        $data['name'] = $this->input->post('name');
        $data['email'] = $this->input->post('email');
        $data['address'] = $this->input->post('address');

        $this->Customers_model->update($data, $id);
        $this->session->set_flashdata('message', 'Cliente \'' . $data['name'] . '\' alterado');

        redirect('/admin/customers', 'refresh');
    }

    $data['customer'] = $this->Customers_model->get($id);
    $data['page'] = $this->config->item('admin_template_dir_admin') . 'edit_customers';
    $data['module'] = 'admin';

    $this->load->view($this->_container, $data);
}

I have tried many things to be able to display the values in the fields while editing the form. The way it is now, if I do a var_dump($customer), shows NULL.

我已经尝试了很多东西,以便在编辑表单时能够在字段中显示值。它现在的方式,如果我做var_dump($ customer),则显示NULL。

1 个解决方案

#1


1  

Please take a look at this code

请看一下这段代码

<?php

class MY_Model extends CI_Model
{
    public $table = 'table_name';
    protected $primary_key = 'id';

    public function __construct()
    {
        parent::__construct();

        $this->load->database();
        $this->load->helper('inflector');
    }

    public function get($id)
    {
        return $this->db->get_where($this->table, array($this->primary_key => $id))->row();
    }

    public function select()
    {
        $data = array();

        $this->db->from($this->table);
        $this->db->order_by($this->primary_key, 'DESC');
        $query = $this->db->get();

        foreach ($query->result_array() as $row) {
            $data[] = $row;
        }

        $query->free_result();

        return $data;
    }

    public function insert($data)
    {
        $data['date_created'] = $data['date_updated'] = date('Y-m-d');
        $data['created_from_ip'] = $data['updated_from_ip'] = $this->input->ip_address();

        if ($this->db->insert($this->table, $data)) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    public function update($data, $id)
    {
        $data['date_updated'] = date('Y-m-d');
        $data['updated_from_ip'] = $this->input->ip_address();

        $this->db->where($this->primary_key, $id);

        return $this->db->update($this->table, $data);
    }

    public function delete($id)
    {
        $this->db->where($this->primary_key, $id);

        return $this->db->delete($this->table);
    }

    public function count_all()
    {
        return $this->db->count_all($this->table);
    }
}

Your controller function

你的控制器功能

public function edit($id)
    {
        $this->load->model('MY_Model');
        if ($this->input->post()) {
            $data['name'] = $this->input->post('name');
            $data['email'] = $this->input->post('email');
            $data['address'] = $this->input->post('address');

            $this->MY_Model->update($data, $id);
            $this->session->set_flashdata('message', 'Cliente \'' . $data['name'] . '\' alterado');

            redirect('/admin/customers', 'refresh');
        }

        $data['customer'] = $this->MY_Model->get($id);
        $data['page'] = $this->config->item('admin_template_dir_admin') . 'edit_customers';
        $data['module'] = 'admin';

        $this->load->view($this->_container, $data);
    } 

#1


1  

Please take a look at this code

请看一下这段代码

<?php

class MY_Model extends CI_Model
{
    public $table = 'table_name';
    protected $primary_key = 'id';

    public function __construct()
    {
        parent::__construct();

        $this->load->database();
        $this->load->helper('inflector');
    }

    public function get($id)
    {
        return $this->db->get_where($this->table, array($this->primary_key => $id))->row();
    }

    public function select()
    {
        $data = array();

        $this->db->from($this->table);
        $this->db->order_by($this->primary_key, 'DESC');
        $query = $this->db->get();

        foreach ($query->result_array() as $row) {
            $data[] = $row;
        }

        $query->free_result();

        return $data;
    }

    public function insert($data)
    {
        $data['date_created'] = $data['date_updated'] = date('Y-m-d');
        $data['created_from_ip'] = $data['updated_from_ip'] = $this->input->ip_address();

        if ($this->db->insert($this->table, $data)) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    public function update($data, $id)
    {
        $data['date_updated'] = date('Y-m-d');
        $data['updated_from_ip'] = $this->input->ip_address();

        $this->db->where($this->primary_key, $id);

        return $this->db->update($this->table, $data);
    }

    public function delete($id)
    {
        $this->db->where($this->primary_key, $id);

        return $this->db->delete($this->table);
    }

    public function count_all()
    {
        return $this->db->count_all($this->table);
    }
}

Your controller function

你的控制器功能

public function edit($id)
    {
        $this->load->model('MY_Model');
        if ($this->input->post()) {
            $data['name'] = $this->input->post('name');
            $data['email'] = $this->input->post('email');
            $data['address'] = $this->input->post('address');

            $this->MY_Model->update($data, $id);
            $this->session->set_flashdata('message', 'Cliente \'' . $data['name'] . '\' alterado');

            redirect('/admin/customers', 'refresh');
        }

        $data['customer'] = $this->MY_Model->get($id);
        $data['page'] = $this->config->item('admin_template_dir_admin') . 'edit_customers';
        $data['module'] = 'admin';

        $this->load->view($this->_container, $data);
    }