I am making a program that deal with multiple tables (recipe, instructions on how to make, directions, etc. ) and I am already able to add new recipe and save the data provided in the input form to the proper table having foreign keys to link each one.
What I did was after the insertion of a new recipe, I would get the latest id of the inserted recipe then get it from the model to the controller and from the controller, I would pass the id to the view like this:
$data['recipe_id'] = $this->recipe_model->id;
$this->load->view('template', $data);
Then in the view, I would be able to have the passed id simply by doing:
<?php echo $recipe_id ?>
I tried to print the id and I did got the right one. Now, I was wondering on how to pass the id to a controller method so that I can query again the information about the new recipe and for me to display it or is my solution is not an efficient one and there is another better way of solving it.
If so, I would be glad to hear from you. Thank you in advance.
You should pass the actual model object, instead of the id:
This way there is no need to call back into a controller to get the recipe information.
If you truly insist, you can implement a helper function or library function that uses the
CIobject to load your model, but you’re much better off simply copying the saved recipe object into the view data.