简体   繁体   中英

CodeIgniter: Array to string conversion error on a page supposed to retrieve data from a table, then edit and update

I'm trying to get records and let users of the application edit details of records and update new details. But when I print_r ($row->emp_name) it returns: John Smith and ($row->emp_type) returns Cachier which are correct values. But why those data are not passed into the 'emp_name' and 'emp_type' inputs in the view?

Second question: I also want to know how to define a value for a dropdown, to be selected on load. Note that the dropdown values has to be from a database and should be retrieved in a similar way described above.

在此处输入图片说明


View:

<?php
    $js = 'id="emp_name"';
    echo form_input('emp_name', $emp_name, set_value('emp_name'), $js);
?>

Controller:

function index() {
    $this->load->model('edit/default_price_model');

    //$data['query'] = $this->default_price_model->get_employees_table();
    $this_employee['emp_name'] = $this->default_price_model->current_cmployees();
    //$temp_array=array_merge($data, $this_employee);

    $this->load->view('/edit/employees', $this_employee);
}

function form_validation() {

    if($this->input->post('this_employee') == "View") {         
        $this->populate_form();
    }
    else {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('emp_name','Employee Name', 'required|min_length[3]|max_length[60]');
        $this->form_validation->set_rules('emp_type','Employee Name', 'required');
        $this->form_validation->set_rules('emp_description','Optional Description', 'max_length[500]');

        if ($this->form_validation->run() == FALSE ) {
            $this->index();
        }       
        else {
            $this->update_table();
        }
    }
}

function populate_form() {
    $this->load->model('edit/default_price_model');
    $selection = $this->input->post('select_employee');
    $data['emp_name'] = $this->default_price_model->get_employees_table($selection);
    $this->index();
}

Model:

function get_employees_table($selection) {
    $query = $this->db->query(" SELECT emp_name, emp_type, emp_description
                                FROM employees 
                                WHERE emp_name='$selection' 
                                ORDER BY emp_name ASC 
                                LIMIT 1");
    if($query->num_rows()>0) {
        foreach($query->result() as $row) {
            $row->emp_name;
            $row->emp_type;
            $row->emp_description;
        }
    }
}

Your form input is just has the form values if set in set_value not your database values it should look like this

<?php
    $js = 'id="emp_name"';
    echo form_input('emp_name', $emp_name,
set_value('emp_name', isset($emp_name[0]->emp_name) ? $emp_name[0]->emp_name : '') 
, $js);
?>

$emp_name is what you have from the controller and has the db values

$data['emp_name'] = $this->default_price_model->get_employees_table($selection);

One thing i have notice you haven't returned any results from the query in your model and also the foreach loop is doing nothing

function get_employees_table($selection) {
    $query = $this->db->query(" SELECT emp_name, emp_type, emp_description
                                FROM employees 
                                WHERE emp_name='$selection' 
                                ORDER BY emp_name ASC 
                                LIMIT 1");
    if($query->num_rows()>0) {
      return $query->result();
    }else{
    return 0;
    }
}

Hope it helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM