![]() |
Passing data from controller to model in an array. Please help! - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Passing data from controller to model in an array. Please help! (/showthread.php?tid=32468) |
Passing data from controller to model in an array. Please help! - El Forum - 07-24-2010 [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( Please help! Passing data from controller to model in an array. Please help! - El Forum - 07-24-2010 [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( You then have a function in the model that accepts the array and processes it from there. Passing data from controller to model in an array. Please help! - El Forum - 07-24-2010 [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 . Passing data from controller to model in an array. Please help! - El Forum - 07-24-2010 [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. Passing data from controller to model in an array. Please help! - El Forum - 07-24-2010 [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(); Passing data from controller to model in an array. Please help! - El Forum - 07-24-2010 [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. Passing data from controller to model in an array. Please help! - El Forum - 07-24-2010 [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 Model: Code: function add($item_info) |