简体   繁体   中英

CodeIgniter Getting the result of a query as a text input value

I wrote a small piece to get a value from a database table according to the input user provides. Quickly here are the events:

  1. User inputs number to an input and submit form
  2. That data are supposed to call the controller, and then the controller have to match and grab relevant data from the database table, and pass it to the controller again.
  3. Then the controller must pass it to the view, where another text input (readonly) picks up the data as the value.

But what I received was an error:

Message: Undefined variable: due_amount Filename: main/new_payment.php Line Number: 148

Line number 148 in new_payment.php is

);

in the View.

This is my Model :

function get_by_room_number($room_data) {       
    $this->db->select('due_amount');
    $query = $this->db->get_where('rooms', array('room_number' => $room_data), 1);

    if($query->num_rows()>0) {
        foreach ($query->result() as $row) {
            return $row->due_amount;
        }
    }

This is the Controller :

function search_by_number() {
    $room_data = $this->input->post('room_number');
    $due_amount = $this->payments_model->get_by_room_number($room_data);
    $this->index();
}

This is the View : (new_payment.php)

<?php echo form_open('payments/search_by_number'); ?>    
<?php $data = array(
    'name'  =>  'total_amount',
    'id'    =>  'appendedPrependedInput',
    'class' =>  'span2',
    'value' =>  $due_amount
    ); // Line Number 148
echo form_input($data);
?>   

<?php echo form_close(); ?>

Try like

$data['due_amount'] = $this->payments_model->get_by_room_number($room_data);

and try to sent it to view like

$this->load->view('view_file',$data);

and at your view file echo it like

echo $due_amount;

assuming that $data is the array that you are passing to your view from your controller function.You cont pass a variable from controller to view.You need to pass it through an array data and then you can get the variable with that variable name

You should assign due_amount to $data variable and pass it to view. Like this:

$data['room_data'] = $this->input->post('room_number');
$data['due_amount'] = $this->payments_model->get_by_room_number($data['room_data']);
$this->load->view('my_view', $data);

Then in view you could do:

print_r($room_data);
print_r($due_amount);

CodeIgniter User Guide might help you understand it better.

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