• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[closed]How to get data from model to controller to view?

#11
[eluser]brucebat[/eluser]
Ok thanks:

Controller:

Code:
// This function displays the submit form for entering Interventional data to the database
            public function displayform()
            {
            
            
                //Checks if a user is logged in, if they are not they get redirected -
                if ( $this->session->userdata('name') == FALSE || $this->session->userdata('access_level') == FALSE)
                    {
                        redirect ('site/index');// to home page
                    }
                
                //Loads table data that will be used to populate a pulldown
                $page['formdata']=$this->submit_model->retrieve_values();
        
    
                
                //This loads the form
                $page['page'] = 'submit';
                $this->load->view('template', $page );
                
                return;
            }

Model

Code:
// Retrieves values that will populate the form e.g. for pull downs etc.
        function retrieve_values()
        {
            $query = $this->db->get('staff');
            
            
            
            
            return $query;
            
        }

View:

Code:
for ($i=0; $i < 5; $i++)
                        {
                            
                            echo form_dropdown ('staff'.$i, $formdata, set_value ('staff'.$i));

                                                }

I also tested the $formdata which came up with the options for the pulldown when I used a foreach and echoed the row, all the 13 rows where displayed in a line on my page.


Thanks

#12
[eluser]cpass78[/eluser]
Your not returning the results of the query, this should work:

Code:
// Retrieves values that will populate the form e.g. for pull downs etc.
    public function retrieve_values() {
        $query = $this->db->get('staff');
        if ($query->num_rows() > 0) { //true if there are rows in the table
            return $query->result(); //returns an object of data
        }
        return false;
    }

#13
[eluser]brucebat[/eluser]
Thanks

However now I am getting this error in my view repeating.

Quote:Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename: helpers/form_helper.php

Line Number: 352

#14
[eluser]cpass78[/eluser]
Are you trying to create 4 dropdown menus? can you post the print_r($formdata);

It looks like one will be sufficient so your view should look like this:
Code:
//$i ought to be an id field in your table
echo form_dropdown ('staff'.$i, $formdata->value, set_value ('staff'.$i)); //$formdata->value is the column in your table that you want to output

#15
[eluser]brucebat[/eluser]
I need five dropdowns as the medical procedures have multiple types of staff in each one.

This is the print dump
Quote:Array ( [0] => stdClass Object ( [staff_id] => 1 [name] => Cardiology Nurse ) [1] => stdClass Object ( [staff_id] => 2 [name] => Radiology Nurse ) [2] => stdClass Object ( [staff_id] => 3 [name] => Scrub Nurse ) [3] => stdClass Object ( [staff_id] => 4 [name] => Circulating Nurse ) [4] => stdClass Object ( [staff_id] => 5 [name] => Nurse ) [5] => stdClass Object ( [staff_id] => 6 [name] => Training Nurse ) [6] => stdClass Object ( [staff_id] => 7 [name] => Physiologist ) [7] => stdClass Object ( [staff_id] => 8 [name] => Radiographer ) [8] => stdClass Object ( [staff_id] => 9 [name] => Consultant ) [9] => stdClass Object ( [staff_id] => 10 [name] => Radiologist ) [10] => stdClass Object ( [staff_id] => 11 [name] => Cardiologist ) [11] => stdClass Object ( [staff_id] => 12 [name] => Anaethestist ) [12] => stdClass Object ( [staff_id] => 13 [name] => Non-medical Staff ) )

I will be wanting to use name.

I tried this

Code:
$formdata->name

But now it is saying

Quote:A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 331

#16
[eluser]cpass78[/eluser]
ok you need some array manipulation here. I think this will do it
Code:
for ( $i = 0; $i < 5; $i++ ){
   foreach( $formdata as $drop_value ) {
   echo form_dropdown ( 'staff'.$i, $drop_value->name, set_value ('staff'.$i) );
    }
}

#17
[eluser]brucebat[/eluser]
Still getting invalid argument supplied for foreach

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 331

Im gonna repost this in a new thread as it isnt related to this one.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.