Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    32
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    + 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.

  • #3
    New Coder
    Join Date
    Feb 2011
    Posts
    32
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #5
    New Coder
    Join Date
    Feb 2011
    Posts
    32
    Thanks
    3
    Thanked 0 Times in 0 Posts
    it worked like a charm.

    Thank you so much.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •