Welcome Guest, Not a member yet? Register   Sign In
[solved]Invalid argument supplied for foreach() - Line 331 form_helper
#1

[eluser]brucebat[/eluser]
Hi all,

I am taking a variable called $formdata that is passed to my view from my controller.

This variable contains all the rows from a table called "Staff" in my database and I want it to populate my pulldown menu in my form.

This is a printf of the variable showing that it contains data:

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 ) )


cpass77 and I have been trying to get it to then put these values in the form_dropdown function however it has been saying this error:

Quote:A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 331


This is the code currently being used in the view:

Code:
for ($i=0; $i < 5; $i++)//creates five rows of fields for the form
                        {
                             foreach( $formdata as $row )
                            {
                                echo form_dropdown ( 'staff'.$i, $row->name, set_value ('staff'.$i) );
                            }
        
                            echo form_input ('staff_quantity'.$i, set_value ('staff_quantity'.$i));
                            echo form_input ('staff_sterilised'.$i, set_value ('staff_sterilised'.$i));
                            echo '<br/>';
                        }

Is this the best way of doing what I am trying to achieve or is there an easier way?

Thanks for your time!
#2

[eluser]Jônatan fróes[/eluser]
Code:
form_dropdown ( 'iam_the_input_name', $iam_an_ array;, $iam_the_selected_value);
#3

[eluser]SitesByJoe[/eluser]
If $formdata is a database result set you need to loop on $formdata->result() or $formdata->result_array() instead of just $formdata. That would cause that error.
#4

[eluser]brucebat[/eluser]
[quote author="SitesByJoe" date="1311795720"]If $formdata is a database result set you need to loop on $formdata->result() or $formdata->result_array() instead of just $formdata. That would cause that error.[/quote]

Already done that in my model here:

Code:
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;
            
        }

Controller:

Code:
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;
            }
#5

[eluser]SitesByJoe[/eluser]
Do a print_r($formdata) to confirm it's something you can loop on...
#6

[eluser]brucebat[/eluser]
Thats what I did , the print dump is in the first post Sad

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

Why is this not working I have tried everything Sad !!!!
#7

[eluser]SitesByJoe[/eluser]
Scratch that - you already are. So then the question is, what is $staff? I don't see it anywhere else in your code. It's proably the $staff . $i that's making the form_helper mad.
#8

[eluser]brucebat[/eluser]
[quote author="SitesByJoe" date="1311796935"]Scratch that - you already are. So then the question is, what is $staff? I don't see it anywhere else in your code. It's proably the $staff . $i that's making the form_helper mad.[/quote]

Im sorry I do not understand what you are saying there is no $staff variable in the view or in the controller or even model.


If you are meaning this :

Code:
'staff'.$i

That basically means "staff" x i = staff1 , staff2, staff3, staff4

Which is the name of the fields in the form.
It is called concatenation.

Thanks
#9

[eluser]SitesByJoe[/eluser]
okay, Line 331 in the form helper is running a foreach() loop on your 2nd parameter of form_dropdown() which is $row->name, but it wants an array for that parameter of the items to populate the dropdown with.

That's the part you need to fix.
#10

[eluser]brucebat[/eluser]
So your saying I need to create an array of $row?

I am totally confused right now :S

Im thinking it would be easier to just remove codeigniters poorly made form_pulldown and just do it in html and just foreach with echo like so, whats your opinions?
Code:
&lt;?php
foreach ($formoptions as $row)
{ ?&gt;
<option value="&lt;?php echo $row->staff_id; ?&gt;">&lt;?php echo $row->name; ?&gt;</option>
&lt;?php
}




Theme © iAndrew 2016 - Forum software by © MyBB