Welcome Guest, Not a member yet? Register   Sign In
Passing data from controller to model in an array. Please help!
#1

[eluser]murphy2006[/eluser]
Hello!

I am trying to pass an array of POST data from a controller into a model.
How is this done? How can I then access the data in the model?

The controller code look like this (is it correct?):

Code:
$result = $this->items_model->add( array(
        
        'ite_owner' => $this->post('user'),  
        'ite_type' => $this->post('type'),
        'ite_value_1' => $this->post('value_1'),
        'ite_value_2' => $this->post('value_2'),
        'ite_value_3' => $this->post('value_3'),
        'ite_value_4' => $this->post('value_4'),
        'ite_value_5' => $this->post('value_5'),
        'ite_access' => $this->post('access')
        
        ));

Please help!
#2

[eluser]rwestergren[/eluser]
Have you already loaded the model?
Code:
$this->load->model('items_model');

Post uses the input class, so you'll need to use:
Code:
$result = $this->items_model->add( array(
        
        'ite_owner' => $this->input->post('user'),  
        'ite_type' => $this->input->post('type'),
        'ite_value_1' => $this->input->post('value_1'),
        'ite_value_2' => $this->input->post('value_2'),
        'ite_value_3' => $this->input->post('value_3'),
        'ite_value_4' => $this->input->post('value_4'),
        'ite_value_5' => $this->input->post('value_5'),
        'ite_access' => $this->input->post('access')
        
        ));

You then have a function in the model that accepts the array and processes it from there.
#3

[eluser]Twisted1919[/eluser]
The $_POST is a global one, so it'll be accessible in your model as well as in your constroller, you don't need to pass it from the controller, only clean it in the model .
#4

[eluser]rwestergren[/eluser]
[quote author="Twisted1919" date="1280004657"]The $_POST is a global one, so it'll be accessible in your model as well as in your constroller, you don't need to pass it from the controller, only clean it in the model .[/quote]

In order to make a more universal model, I'd pass the array to the controller. That way you can use the model function with other data sources as well.
#5

[eluser]Twisted1919[/eluser]
[quote author="rwestergren" date="1280011597"][quote author="Twisted1919" date="1280004657"]The $_POST is a global one, so it'll be accessible in your model as well as in your constroller, you don't need to pass it from the controller, only clean it in the model .[/quote]

In order to make a more universal model, I'd pass the array to the controller. That way you can use the model function with other data sources as well.[/quote]

True . I thought he means that the $_POST data will be used only in the model .

Anyway, one can do like
Code:
$cleaned_post = array();
foreach($_POST AS $key=>$value)
{
  $cleaned_post[$key] = $this->input->post($value,TRUE);
  $$key = $cleaned_post[$key];//so that we can use the variables as well, not only the array .
}

$this->some_model->work_with_data($cleaned_post);

$this->data['username'] = $username ;//assuming that an input field had the name username.
#6

[eluser]murphy2006[/eluser]
Thanks Twisted1919. I think I am close to getting it. What I want is to pass an array of data from a controller into a model and in that model I need to use the data to insert it into a database.

Sure, I could set each data item as a parameter to the model but I got 10 of them and it would not look nice.

I am not really sure how to use your example.
#7

[eluser]rwestergren[/eluser]
[quote author="murphy2006" date="1280033440"]Thanks Twisted1919. I think I am close to getting it. What I want is to pass an array of data from a controller into a model and in that model I need to use the data to insert it into a database.

Sure, I could set each data item as a parameter to the model but I got 10 of them and it would not look nice.

I am not really sure how to use your example.[/quote]

Both examples are passing the array as a whole, not individual parameters. Maybe to better understand, look at it this way.

Controller:

Code:
//Create array first
$item_info = array(
        'ite_owner' => $this->input->post('user'),  
        'ite_type' => $this->input->post('type'),
        'ite_value_1' => $this->input->post('value_1'),
        'ite_value_2' => $this->input->post('value_2'),
        'ite_value_3' => $this->input->post('value_3'),
        'ite_value_4' => $this->input->post('value_4'),
        'ite_value_5' => $this->input->post('value_5'),
        'ite_access' => $this->input->post('access'));

//Pass to model
$this->items_model->add($item_info);

Model:
Code:
function add($item_info)
{
   //Access as array here
   $owner = $item_info['ite_owner'];
  
   //insert array into DB
   $this->db->insert('table_name', $item_info);
  
}




Theme © iAndrew 2016 - Forum software by © MyBB