CodeIgniter Forums
why is this code failing? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: why is this code failing? (/showthread.php?tid=76903)



why is this code failing? - richb201 - 07-01-2020

I am trying to create an array that I will use to "insert" 2 fields into a table. I keep two important fields in a session variable. I want to insert them into a mySQL table. There seems to be a problem with the userdata('campaign'). Why is that?

$data = array(
    'email' => $this->session->userdata('userid'),
    'campaign' => '$this->session->userdata('campaign')
);


$this->db->insert($file_name, $data);

and I get this:
Type: ParseError

Message: syntax error, unexpected 'campaign' (T_STRING), expecting ')'

Filename: /app/application/libraries/Image_CRUD.php
Line Number: 368


RE: why is this code failing? - neuron - 07-01-2020

As error says it is a SYNTAX error.
Remove quote character before $this


using session data between a controller and a library - richb201 - 07-01-2020

Shy Thanks. How stupid of me. What I am really trying to do is add a few more fields into the table.

      $insert = array($this->url_field => $file_name);
      array_push($insert,'email => ' . $this->session->userdata('userid'));  <<trying to add two more fields before doing the insert
      array_push($insert,'campaign =>' .  $this->session->userdata('campaign'));
      $this->ci->db->insert($this->table_name, $insert);

This is failing with "call to a member function userdata() on null". So it seems that my image_CRUD.php module (which is in libraries) cannot load the session data. I tried adding $this->load->library('session'); to the Constructor in image_CRUD.php  but that fails. Since session is related to cookies, I figure that won't work either.  I tried a call (within image_CRUD.php) to $campaign= $this->session->userdata('campaign'); but this returns an error "Call to a member function userdata() on null". Any idea how to fix this? 

The problem is that I need to get those two variables, email and campaign into two  columns in the database record and can't seem to get them transferred to a different module. Any idea how to do this? 




also, where did the docs for CI 3 go??


RE: why is this code failing? - InsiteFX - 07-03-2020

If you have CI 3 the docs come with it only the developer download needs them complied.

Go to user_guide and click on index.html theirs your docs.


RE: why is this code failing? - ivantcholakov - 07-03-2020

If you are outside of context of a controller or a model, try this:

Code:
get_instance()->load->library('session');

But it would be preferable not to desperse usage of the session library within modules, code would become hardly testable, it would be better you to read this data form a controller and pass it to a model.


RE: why is this code failing? - richb201 - 07-03-2020

I ended up getting his $_SERVER[email]. I had no idea that it we be there and just stumbled onto it. Re


RE: why is this code failing? - richb201 - 07-06-2020

I managed to get it working using $_SERVER[email]. That variable is available in the other module. Works great thx.