CodeIgniter Forums
CI Objects and arrays - aaghh! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: CI Objects and arrays - aaghh! (/thread-31759.html)



CI Objects and arrays - aaghh! - El Forum - 06-30-2010

[eluser]Loquela[/eluser]
I think I'm missing the wood for the trees here.

I'm struggling with retrieving values for arrays and objects. Please help.

In my controller I have:

Code:
$data = array('upload_data' => $this->upload->data());

and I am able to list the upload_data using a loop:

Code:
foreach($upload_data as $item => $value):, etc, etc



This works fine.

But what I can't seem to do is capture $orig_name from the $upload_data variable for my model. I'm doing this:

controller:
Code:
$this->files_model->insert_file($data);

model:
Code:
function insert_file($data)
     {
      
           $this->db->insert('als_files', array('file_filename' => $upload_data['orig_name']));
     }
I know this is wrong and opf course i get "Undefined variable: upload_data"

Could anyone help?

Many thanks in advance,

L.


CI Objects and arrays - aaghh! - El Forum - 06-30-2010

[eluser]Simian Studios[/eluser]
The way the view handles data and the way the model handles data are different. The view automatically parses the $data array into simple variables for use, whereas the model doesn't.

If you pass $data['upload_data'] to your model instead of the whole $data array, it should work.


CI Objects and arrays - aaghh! - El Forum - 06-30-2010

[eluser]Loquela[/eluser]
Thanks for this Simian,

Do you mean
model:
Code:
function insert_file($data['upload_data'])

This produces a "Parse error: parse error, expecting `')'"

L.


CI Objects and arrays - aaghh! - El Forum - 06-30-2010

[eluser]Simian Studios[/eluser]
[quote author="Loquela" date="1277931804"]
Do you mean:
Code:
function insert_file($data['upload_data'])
[/quote]

Not quite.. in the controller:

Code:
$this->files_model->insert_file($data['upload_data']);

And then make sure in the model that you are referencing the correct variable. E.g.

Code:
function insert_file($data)
{    
    $this->db->insert('als_files', array('file_filename' => $data['orig_name']));
}



CI Objects and arrays - aaghh! - El Forum - 06-30-2010

[eluser]Hernando[/eluser]
$this->files_model->insert_file($data['upload_data']);

[quote author="Loquela" date="1277931804"]Thanks for this Simian,

Do you mean
model:
Code:
function insert_file($data['upload_data'])

This produces a "Parse error: parse error, expecting `')'"

L.[/quote]


CI Objects and arrays - aaghh! - El Forum - 07-01-2010

[eluser]Loquela[/eluser]
Thanks Simian,

Sorted!

L.


CI Objects and arrays - aaghh! - El Forum - 07-01-2010

[eluser]Simian Studios[/eluser]
[quote author="Loquela" date="1277990273"]Thanks Simian,

Sorted!

L.[/quote]

Cool, no worries Smile Thanks for the blog comment too, have corrected the post now Wink


CI Objects and arrays - aaghh! - El Forum - 07-01-2010

[eluser]Loquela[/eluser]
:coolsmile:

Sharp!