joining variables together

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian Davies

    joining variables together

    Hello
    I am trying to update a session, ie everytime the page is resubmitted I want
    the current selected value from a list to be appended to the old session
    value. I have the following code but am unable to get it to work. I dont
    know the correct syntac wher I have put the word 'PLUS'.

    <?php $_SESSION['savedcomm'] = $_SESSION['savedcomm'] PLUS
    $_POST['selectcomm'];?>

    I have tried & but this doesnt work, or even && which I have seen in some
    script

    Help please
    Ian



  • J.O. Aho

    #2
    Re: joining variables together

    Ian Davies wrote:[color=blue]
    > Hello
    > I am trying to update a session, ie everytime the page is resubmitted I want
    > the current selected value from a list to be appended to the old session
    > value. I have the following code but am unable to get it to work. I dont
    > know the correct syntac wher I have put the word 'PLUS'.
    >
    > <?php $_SESSION['savedcomm'] = $_SESSION['savedcomm'] PLUS
    > $_POST['selectcomm'];?>
    >
    > I have tried & but this doesnt work, or even && which I have seen in some
    > script[/color]

    $_SESSION['savedcomm'] = $_SESSION['savedcomm'] . $_POST['selectcomm'];



    //Aho

    Comment

    • Ian Davies

      #3
      Re: joining variables together

      Thanks Aho

      sorry I should have added, what if I want each newly added item to the
      session to display as seperat lines when returned to a text field?

      Ian

      "J.O. Aho" <user@example.n et> wrote in message
      news:4e0flvF1cb jk5U1@individua l.net...[color=blue]
      > Ian Davies wrote:[color=green]
      > > Hello
      > > I am trying to update a session, ie everytime the page is resubmitted I[/color][/color]
      want[color=blue][color=green]
      > > the current selected value from a list to be appended to the old session
      > > value. I have the following code but am unable to get it to work. I dont
      > > know the correct syntac wher I have put the word 'PLUS'.
      > >
      > > <?php $_SESSION['savedcomm'] = $_SESSION['savedcomm'] PLUS
      > > $_POST['selectcomm'];?>
      > >
      > > I have tried & but this doesnt work, or even && which I have seen in[/color][/color]
      some[color=blue][color=green]
      > > script[/color]
      >
      > $_SESSION['savedcomm'] = $_SESSION['savedcomm'] . $_POST['selectcomm'];
      >
      >
      >
      > //Aho[/color]


      Comment

      • David Haynes

        #4
        Re: joining variables together

        Ian Davies wrote:[color=blue]
        > Thanks Aho
        >
        > sorry I should have added, what if I want each newly added item to the
        > session to display as seperat lines when returned to a text field?
        >
        > Ian[/color]

        $lines = $_SESSION['line1'].'<br>'.$_SESSI ON['line2'];
        or
        $lines = sprintf("%s<br> %s", $_SESSION['line1'], $_SESSION['line2']);
        or
        $prefix = 'line';
        $lines = '';
        foreach($_SESSI ON as $key => $value) {
        if( substr($key, 0, strlen($prefix) ) == $prefix) {
        if( $lines == '' ) $lines = $value;
        else $lines = sprintf("%s<br> %s", $lines, $value);
        }
        }

        This assumes that all the lines you want to join together begin with the
        tag name 'line'. (e.g. line1, line2, line_foo, etc.)

        -david-

        Comment

        • J.O. Aho

          #5
          Re: joining variables together

          Ian Davies wrote:
          [color=blue]
          > sorry I should have added, what if I want each newly added item to the
          > session to display as seperat lines when returned to a text field?[/color]


          $_SESSION['savedcomm'] = $_SESSION['savedcomm'] . "\n" . $_POST['selectcomm'];

          This would add new line between the old and new data.

          If you want to display it in a html section, then you can use nl2br()
          Inserts HTML line breaks before all newlines in a string


          <h2><?php echo nl2br($_SESSION['savedcomm']); ?></h2>

          If you add things to a textarea, then do

          <textarea name="thetext" rows="20" cols="80"><?php echo
          $_SESSION['savedcomm']; ?></textarea>

          Here you don't use the nl2br as the the textarea element does respect the new
          line, which isn't done in html otherwise.


          //Aho

          Comment

          • Ian Davies

            #6
            Re: joining variables together

            Thanks J.O.
            That was what I wanted

            "J.O. Aho" <user@example.n et> wrote in message
            news:4e0ksjF1ci e9vU1@individua l.net...[color=blue]
            > Ian Davies wrote:
            >[color=green]
            > > sorry I should have added, what if I want each newly added item to the
            > > session to display as seperat lines when returned to a text field?[/color]
            >
            >
            > $_SESSION['savedcomm'] = $_SESSION['savedcomm'] . "\n" .[/color]
            $_POST['selectcomm'];[color=blue]
            >
            > This would add new line between the old and new data.
            >
            > If you want to display it in a html section, then you can use nl2br()
            > http://www.php.net/manual/en/function.nl2br.php
            >
            > <h2><?php echo nl2br($_SESSION['savedcomm']); ?></h2>
            >
            > If you add things to a textarea, then do
            >
            > <textarea name="thetext" rows="20" cols="80"><?php echo
            > $_SESSION['savedcomm']; ?></textarea>
            >
            > Here you don't use the nl2br as the the textarea element does respect the[/color]
            new[color=blue]
            > line, which isn't done in html otherwise.
            >
            >
            > //Aho[/color]


            Comment

            Working...