Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-18-2012, 11:38 PM   PM User | #1
frank5050
New Coder

 
Join Date: Feb 2011
Posts: 32
Thanks: 3
Thanked 0 Times in 0 Posts
frank5050 is an unknown quantity at this point
Simple Newbie Sessions and insert Problem

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
frank5050 is offline   Reply With Quote
Old 10-19-2012, 12:19 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
+ only works for numerical addition in PHP (oh, and arrays to an extent as well). You need to concat usint the . instead.
PHP Code:
$var1 ', ' $var2 ', ' $var3 ', . . .' 
Or you can use a string or printformatter. I prefer formatters myself:
PHP Code:
$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.
Fou-Lu is offline   Reply With Quote
Old 10-19-2012, 12:29 AM   PM User | #3
frank5050
New Coder

 
Join Date: Feb 2011
Posts: 32
Thanks: 3
Thanked 0 Times in 0 Posts
frank5050 is an unknown quantity at this point
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.
frank5050 is offline   Reply With Quote
Old 10-19-2012, 12:31 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Nope, you can simply replace your variables directly in place:
PHP Code:
 $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.
Fou-Lu is offline   Reply With Quote
Old 10-19-2012, 12:38 AM   PM User | #5
frank5050
New Coder

 
Join Date: Feb 2011
Posts: 32
Thanks: 3
Thanked 0 Times in 0 Posts
frank5050 is an unknown quantity at this point
it worked like a charm.

Thank you so much.
frank5050 is offline   Reply With Quote
Reply

Bookmarks

Tags
insert, mysql php, sessions

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:25 AM.


Advertisement
Log in to turn off these ads.