Welcome Guest, Not a member yet? Register   Sign In
[closed]How to get data from model to controller to view?
#1

[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()
        {
            
            //The criteria for the where is set by using the user ID stored in session
            $user_criteria = array ( 'user_id' => $this->session->userdata('userID'));
            
                $user_data = $this->db->get_where('user', $user_criteria);
                
                if ($user_data->num_rows() > 0)
                        {
                           foreach ($user_data->result() as $row)
                           {
                              $row->user_id;
                              $row->first_name;
                              $row->second_name;
                              $row->location;
                              $row->country;
                              $row->organisation;
                              $row->email;
                              $row->access_level;
                           }
                        }
                    
                    
                    
            return;
        }

I tried doing this in the controller but It has not worked:

Code:
ublic function user_data()
        {
        //store data from row created by model function
        $page['userdata'] = $this->control_panel_model->get_user_data();
            
                        //page name to load in template view
            $page['page'] = 'cp/userdata';
            $this->load->view('template', $page);
        
        }
#2

[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');
        //The criteria for the where is set by using the user ID stored in session  
        $user_data = $this->db->get_where ( 'user', $userid );
        if ($user_data->num_rows () > 0) {
            return $user_data->result ();
        }
        return false;
    }

Then in your controller you can do:
Code:
public function user_data() {
        //store data from row created by model function
        $userid = $this->session->userdata('userID');
        $page ['userdata'] = $this->control_panel_model->get_user_data ($userid);
        //page name to load in template view
        $page ['page'] = 'cp/userdata';
        $this->load->view ( 'template', $page );
    }

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
#3

[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)
                        {
                           foreach ($page['userdata']->result() as $row)
                           {
                              echo $row->user_id;
                              echo$row->first_name;
                              echo$row->second_name;
                              echo$row->location;
                              echo$row->country;
                              echo$row->organisation;
                              echo$row->email;
                              echo$row->access_level;
                           }
                        }
~

Thanks for your help so far!
#4

[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) {
foreach ($userdata as $row) {
    echo $row->user_id;
    echo $row->first_name;
    echo $row->second_name;
    echo $row->location;
    echo $row->country;
    echo $row->organisation;
    echo $row->email;
    echo $row->access_level;
    }
}else{
echo 'Oops, nothing exists!';
}

Something like that should work
#5

[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 Smile
#6

[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/
#7

[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.
#8

[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 Smile[/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
#9

[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
Resource ID#39
Array
Array
Array
0
13

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
Doctor
Surgeon
Consultant


This is my variable that is passed to the view:
Code:
$staffoptions //variable passed from controller

echo form_dropdown('staff_pulldown', $staffoptions, set_value('staff_pulldown'));

Im guessing I need to prepare it in someway?
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB