0

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(); ?>

2 Answers 2

1

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

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

Comments

1

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.

1 Comment

Good luck with your project.

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.