Welcome Guest, Not a member yet? Register   Sign In
Stuck using form data in my model. Message: Array to string conversion
#1

[eluser]ChrisF79[/eluser]
I have a simple form getting passed to my "calculator" controller. That controller looks like this:
Code:
function calculator() {

  $data['userage'] = $this->input->post('userage');
  $data['usercigs'] = $this->input->post('usercigs');
  $data['usercost'] = $this->input->post('usercost');
  
  // Load The Model
  $this->load->model('calculator_model', $data);
  $data['output'] = $this->calculator_model->calculateSavings($data[userage], $data[usercost], $data[usercigs]);
  
  $this->load->view('calculator', $data);
}

In my model, I have:
Code:
class Calculator_model extends CI_Model {
function calculateSavings($userage, $usercost, $usercigs) {
  // Define Some Variables
  $costPerCig = $usercost / 20;
  $monthlyCost = $CostPerCig * $usercigs * 30;

  return $monthlyCost;
}
}
Unfortuantely, when I run this I get:
Code:
A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: core/Loader.php

Line Number: 270
Code:
A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: core/Loader.php

Line Number: 303
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Site::$calculator_model

Filename: controllers/site.php

Line Number: 35

I have no idea how I'm supposed to be passing these variables. Thanks!
#2

[eluser]alsemany[/eluser]
$this->load->model('calculator_model');

instead of

$this->load->model('calculator_model', $data);
#3

[eluser]CI_expert_indian[/eluser]
Exactly you have to use $this->load->model(‘calculator_model’) because you can not pass data to any model while loading . You can pass data to function at calling as parameters




Theme © iAndrew 2016 - Forum software by © MyBB