Calling one variable as part of another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • StuButler
    New Member
    • May 2007
    • 3

    Calling one variable as part of another

    Hi,

    I have a problem, i can't work out how to produce a variable with another variable in it.

    e.g.
    Code:
    $username=$_POST['username'];
    i want a variable ($content) which will then make a statement like "your username is $username"

    It must be a variable as this is loaded into another page dynamically.

    How to do i write the $content = '????????

    Hope someone can help.

    I tried using
    Code:
    $content ='your username is <?php echo $username ?>';
    but it doesnt work, i assume that was a crazy thing to try.
  • ankitmathur
    New Member
    • May 2007
    • 36

    #2
    Re: Calling one variable as part of another

    Hi,

    This is how you should be writing the code.
    [PHP]
    <?php
    $username="Anki t Mathur";
    echo $content = "Your username is ". $username;
    ?>
    [/PHP]

    Happy Coding.....
    Ankit Mathur


    Originally posted by StuButler
    Hi,

    I have a problem, i can't work out how to produce a variable with another variable in it.

    e.g.
    Code:
    $username=$_POST['username'];
    i want a variable ($content) which will then make a statement like "your username is $username"

    It must be a variable as this is loaded into another page dynamically.

    How to do i write the $content = '????????

    Hope someone can help.

    I tried using
    Code:
    $content ='your username is <?php echo $username ?>';
    but it doesnt work, i assume that was a crazy thing to try.
    Last edited by ankitmathur; May 22 '07, 11:11 AM. Reason: Mis-printed word

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      This can be done in many ways.

      Most basic, like ankitmathur mentioned
      [code=php]
      echo "My name is ". $name;
      [/code]

      You can also put it inside the string. PHP will realize you are refering to a variable and exchange it for it's value.
      Be carefull not to user single quote marks
      they do not evaluate variable names and print the variable name rather than its value.
      [code=php]
      echo "My name is $name";
      [/code]

      You can also put arrays inside the quote marks if you put {} arround them.
      [code=php]
      echo "My name is {$_POST['name']}";
      [/code]

      And if special cases, if all else fails you could replace some marker with the text you want.
      [code=php]
      echo str_replace("!-name-!", $name, "Hi my name is !-name-!");
      [/code]

      Hope this helps

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        I think you are trying to pass the $username variable over the pages.

        So create a session variable and assign the $username to the session.
        Then later on you can execute that value on a different page. until you close the browser window.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by ajaxrand
          I think you are trying to pass the $username variable over the pages.

          So create a session variable and assign the $username to the session.
          Then later on you can execute that value on a different page. until you close the browser window.
          If this is the case, this article could help.

          Comment

          • StuButler
            New Member
            • May 2007
            • 3

            #6
            The username was an example, perhaps i should explain my full situation.

            I get these values from an input form:

            Code:
            $title=$_POST['trackname'];
            $author=$_POST['author'];
            $region=$_POST['region'];
            $version=$_POST['version'];
            $website=$_POST['website'];
            $stars=$_POST['stars'];
            $details=$_POST['details'];
            $ch_rl_mgp=$_POST['ch_rl_mgp'];
            $ch_rl_sbk=$_POST['ch_rl_sbk'];
            $ch_rl_bsb=$_POST['ch_rl_bsb'];
            $ch_rl_ama=$_POST['ch_rl_ama'];
            $ch_rl_aus=$_POST['ch_rl_aus'];
            $ch_rl_jpn=$_POST['ch_rl_jpn'];
            $ch_gp_mgp500=$_POST['ch_gp_mgp500'];
            $ch_gp_stonl=$_POST['ch_gp_stonl'];
            $date=date("Y-m-d");
            $ident=ereg_replace(' ', '_', $title);
            I use an include function to load a design of a page (to avoid having to code designs all the time)

            Code:
            include("../design/body/twx_rightpane.php");
            The twx_rightpane.p hp contains the following variable:

            Code:
            <?php echo $mainrightpane ?>
            Which is the variable in my original php page which needs to have the other variables in it:

            Code:
            $mainrightpane='echo "Title: $title <br />Author: $author <br />Region: $region <br />Version: $version <br />Website: $website <br />STARS: $stars <br />DETAILS: $details <br />RLCHAMPS:  $ch_rl_mgp $ch_rl_sbk $ch_rl_bsb $ch_rl_ama $ch_rl_aus $ch_rl_jpn <br />GPCHAMPS: $ch_gp_mgp500 $ch_gp_stonl <br />Date: $date <br />IDENT: $ident <br /><br />";';
            }
            However, the method above doesnt work.

            Stu

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              The problem is how you create the $mainrightpane string.

              Using varibale names in strings created with single quotation marks will not output the variable's value as you are trying to do, it outputs the string exactly as you write it without evaluating any variables.

              Consider the following:
              [CODE=php]

              $name = "Atli";
              echo 'My name is $name';// Outputs: My name is $name
              echo "My name is $name";// Outputs: My name is Atli

              // This is similar to your $mainrightpane variable.
              $mrp = 'echo "My name is $name";';
              echo $mrp; // Outputs: echo "My name is $name";

              // I don't reccomend this, but it is possible.
              eval($mrp); // Outputs: My name is Atli
              [/CODE]

              Your $mainrightpane variable contains PHP code, but because you are creating it with single quote marks and then sending it using the echo function, it is not being evaluated as PHP but rather sent as HTML to the client browser.

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Take two of these, and post back in the morning:

                Comment

                • ak1dnar
                  Recognized Expert Top Contributor
                  • Jan 2007
                  • 1584

                  #9
                  Your question is still not clear for me, Anyway if you went through the posts submitted by Atli and pbmods Clearly its explaining the way of echoing String variables.

                  Let me add something for the thread. As you mentioned your $_POST Strings is coming from a Different page and it will store the values in this page.I'll name it main.php .

                  main.php

                  [PHP]<?php
                  $title='Title Goes Here';
                  $author=$_POST['author'];
                  $region=$_POST['region'];
                  $version=$_POST['version'];
                  $website=$_POST['website'];
                  $stars=$_POST['stars'];
                  $details=$_POST['details'];
                  $ch_rl_mgp=$_PO ST['ch_rl_mgp'];
                  $ch_rl_sbk=$_PO ST['ch_rl_sbk'];
                  $ch_rl_bsb=$_PO ST['ch_rl_bsb'];
                  $ch_rl_ama=$_PO ST['ch_rl_ama'];
                  $ch_rl_aus=$_PO ST['ch_rl_aus'];
                  $ch_rl_jpn=$_PO ST['ch_rl_jpn'];
                  $ch_gp_mgp500=$ _POST['ch_gp_mgp500'];
                  $ch_gp_stonl=$_ POST['ch_gp_stonl'];
                  $date=date("Y-m-d");
                  $ident=ereg_rep lace(' ', '_', $title);

                  include("twx_ri ghtpane.php");

                  echo $mainrightpane;
                  ?>
                  [/PHP]

                  You have to put this include statement after assigning the vales to the $_POST String. Normally Object oriented languages get execute line by line.
                  if you are adding this line [PHP]include("twx_ri ghtpane.php");[/PHP]

                  on the top of the page you wont be able to pass the $_POST strings to the $mainrightpane that resides under twx_rightpane.p hp.

                  and for the twx_rightpane.p hp try to echo the string values like this.

                  twx_rightpane.p hp
                  [PHP]<?php
                  $mainrightpane= '
                  Title: '.$title.' <br />
                  Author: '.$author.' <br />
                  Region: '.$region.' <br />
                  Version: '.$version.' <br />
                  Website: '.$website.' <br />
                  ';

                  ?>[/PHP]

                  Comment

                  • StuButler
                    New Member
                    • May 2007
                    • 3

                    #10
                    Sorted, was the double quotations, thanks.

                    Stu

                    Comment

                    • llntrvr
                      New Member
                      • Jun 2007
                      • 1

                      #11
                      it needs to be like
                      Code:
                      $content ="your username is $username";

                      Comment

                      • eragon
                        Contributor
                        • Mar 2007
                        • 431

                        #12
                        put username in a session...
                        put this before ANY html:
                        [PHP]<?php
                        session_start() ;
                        $_SESSION['userid'] = $_POST['username'];
                        ?>[/PHP]and in the html you put:
                        [PHP]<?php
                        echo ('Your username is: '.$_SESSION['userid'].'. Welcome.');
                        ?>[/PHP]
                        and this username will stay in the browsers cache untill you call this function:

                        [PHP]<?php
                        unset($_SESSION['userid']);
                        ?>[/PHP]
                        and remember, ANY page that uses this session string make sure you put the

                        [PHP]<?php
                        session_start() ;
                        ?>[/PHP]before the html or you will get a nasty error.

                        ok? is this useful? it is nice, because you can use the username to verify that the user is logged in. if you want to know how, just ask.

                        Comment

                        Working...