View Full Version : Simple Newbie Sessions and insert Problem

10-19-2012, 12:38 AM

I am new to php and working on a bit of code.

I have 2 session variables and I can retrieve the value as below:


I need to insert both these variables in the database along with a comments fields which is being inserted fine by itself given below:

This works fine:
$data['comment'] = $this->session->data['comment'];

what I need to do is to also insert the 'phonemake' and 'phonemodel' data along with the above comments data.

I would like to do this:

$data['comment'] = $this->session->data['comment'] + $this->session->data['phonemake'] + $this->session->data['phonemodel'];

The one variable comment by itself is being inserted just fine. What is the correct syntax and way to make phonemake and phonemodel insert along with comments.


10-19-2012, 01:19 AM
+ only works for numerical addition in PHP (oh, and arrays to an extent as well). You need to concat usint the . instead.

$var1 . ', ' . $var2 . ', ' . $var3 . ', . . .'

Or you can use a string or printformatter. I prefer formatters myself:

$data['comment'] = sprintf('%s, %s, %s', $var1, $var2, $var3);

But I'd only suggest that if you need to separate the data in some way. If you don't, just use a plain . concat instead.

10-19-2012, 01:29 AM
Should I simply replace $var1 by $this->session->data['phonemake'] and $var2 by $this->session->data['phonemodel'] or do I have to declare it first like this.

$var1 = $this->session->data['phonemake'];
$var2 = $this->session->data['phonemodel'];

I'm confused as to what to put in place of $var1 and $var2 you stated above.

I'm totally new to this, so I'll appreciate the help.

10-19-2012, 01:31 AM
Nope, you can simply replace your variables directly in place:

$data['comment'] = $this->session->data['comment'] . ', ' . $this->session->data['phonemake'] . ', ' . $this->session->data['phonemodel'];

Which would give you a string of the three variables concatenated together and separated with , between each field.

10-19-2012, 01:38 AM
it worked like a charm.

Thank you so much.