...

View Full Version : Simple Newbie Sessions and insert Problem



frank5050
10-18-2012, 11:38 PM
Hi,

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:

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

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.

Thanks

Fou-Lu
10-19-2012, 12: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.

frank5050
10-19-2012, 12: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.

Fou-Lu
10-19-2012, 12: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.

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

Thank you so much.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum