[closed]How to get data from model to controller to view? |
[eluser]brucebat[/eluser]
Hi all, This is my first time ever doing CRUD operations and I am a bit unsure about how to tackle this. In the end I am wanting to have my values put in form which can then be edited by the user so they can update email, etc. I have my controller called "control_panel" this will eventually let the user perform CRUD on records they have submitted and CRUD on their personal details. I am trying to do the easy stuff at the moment and firstly retrieve the users details and just display them on a page however I am having trouble getting them from the model to the view. Code: $this->load->view('template',$page) The problem is you can only pass one variable (in my case called $page) in the function above. I have my model accessing the data. So how can I get this data which is ready to be passed from my model to my controller and then my view? Code: public function get_user_data() I tried doing this in the controller but It has not worked: Code: ublic function user_data()
[eluser]cpass78[/eluser]
Well your foreach in your model isnt doing anything, you not sending anything back to the controller. I migth write it like this: Code: public function get_user_data($userid) { //pass $userid in from the controller like $userid = $this->session->userdata('userID'); Then in your controller you can do: Code: public function user_data() { From there you can just loop through the $userdata in your view and access the objects like you were trying to do in your model's foreach Of course you should check the value of $page ['userdata'] for false then send it to the view
[eluser]brucebat[/eluser]
Thanks for the help: So say I wanted to just echo out the values into my view I have tried this and it is not liking it saying: Quote:Fatal error: Call to a member function result() on a non-object Code: if ($page['userdata'] != FALSE) Thanks for your help so far!
[eluser]cpass78[/eluser]
Ok, in laymans terms codeigniter uses a method called extract to create variables from arrays passed to the views, so calling $this->load->view('viewname', $page) sends the $page array to the view and all the keys are the new variables, so $page['userdata'] when it get to the view is actually a variable in the view called $userdata, understand? So with that in in mind your view code should look like this: Code: if (FALSE !== $userdata) { Something like that should work
[eluser]brucebat[/eluser]
Just out of curiosity. So the $userdata is in an array type yes? So for functions in codeigniter like the dropdown function which can accept arrays it would be ok to pass them in? Or would I need to do some form of preparation beforehand and then pass them to the function. E.g. Model: gets from a database table all rows and stores them in an array called "$options" Controller: recieves the array and passes it to the view using the parameter $page View: echo form_pulldown('myoptionlist', $options); Thanks for your informative responses, I am learning alot
[eluser]Unknown[/eluser]
@brucebat I would watch the videos @nettuts - they helped me a lot http://net.tutsplus.com/articles/news/co...tch-day-1/
[eluser]cpass78[/eluser]
[quote author="rmmsteve" date="1311634622"]@brucebat I would watch the videos @nettuts - they helped me a lot http://net.tutsplus.com/articles/news/co...tch-day-1/[/quote] agreed, thats about how i started to learn CI. Well that and alot of trial and error.
[eluser]cpass78[/eluser]
[quote author="brucebat" date="1311633397"]Just out of curiosity. So the $userdata is in an array type yes? So for functions in codeigniter like the dropdown function which can accept arrays it would be ok to pass them in? Or would I need to do some form of preparation beforehand and then pass them to the function. E.g. Model: gets from a database table all rows and stores them in an array called "$options" Controller: recieves the array and passes it to the view using the parameter $page View: echo form_pulldown('myoptionlist', $options); Thanks for your informative responses, I am learning alot [/quote] Yes the $userdata is an array but its an object because the model returned result() not result_array() check this You can pass it into the form_dropdown function but you should make sure it its populated with data first of course. The example you give is correct
[eluser]brucebat[/eluser]
Hey all, I managed to get my data to the view succesfully however when I place it in a pulldown It is coming up with strange options instead of what it should be: Strange options are: Quote:Resource ID#30 I checked there is data as I did an foreach array echo and there are values: What should be in my pulldown is this: There should be 13 options: Quote:Nurse This is my variable that is passed to the view: Code: $staffoptions //variable passed from controller Im guessing I need to prepare it in someway?
[eluser]cpass78[/eluser]
So you called $this->load->view ( 'template', $page ); in the controller and in the view you are trying to output $staffoptions. $staffoptions is defined like $page['staffoptions'] presumably in the controller? Probably easier to post your mvc code. |
Welcome Guest, Not a member yet? Register Sign In |