1

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.

enter image description here


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;
        }
    }
}

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

It almost worked. Now when I print_r it results an array. i.imgur.com/JNrFGDP.png :: How can I fill Employee Name input with [emp_name] in the resulted array?
As i already mentioned in my answer $emp_name[0]->emp_name contains the name in your view i have just wrap it in the isset check you can directly put it in form_input('emp_name', $emp_name,set_value('emp_name', $emp_name[0]->emp_name ) , $js)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.