Is something like this possible with php?

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

    Is something like this possible with php?

    Hi,

    I am trying to see if this is possible. I have thought about it and
    cant see an easy way to do this.


    OK say I have a script

    <?php

    //these variables get set in the script

    $fname='Moe';
    $bday='1970';

    //extract a field from mysql
    //select someField from someTable limit 1;
    //the variable someField contains this value

    //$someField='Wel come back $fname ! wow you must be an old fart since
    you were born in $bday'

    As you can see the variable is stored in the mysql table with embedded
    variables in the string itself. What I would like to do is have these
    variables evaluated somehow so that the $someField value contains this
    value

    Welcome back Moe ! wow you must be an old fart since you were born in
    1970


    ?>
  • Justin Koivisto

    #2
    Re: Is something like this possible with php?

    dan wrote:
    [color=blue]
    > Hi,
    >
    > I am trying to see if this is possible. I have thought about it and
    > cant see an easy way to do this.
    >
    >
    > OK say I have a script
    >
    > <?php
    >
    > //these variables get set in the script
    >
    > $fname='Moe';
    > $bday='1970';
    >
    > //extract a field from mysql
    > //select someField from someTable limit 1;
    > //the variable someField contains this value
    >
    > //$someField='Wel come back $fname ! wow you must be an old fart since
    > you were born in $bday'
    >
    > As you can see the variable is stored in the mysql table with embedded
    > variables in the string itself. What I would like to do is have these
    > variables evaluated somehow so that the $someField value contains this
    > value
    >
    > Welcome back Moe ! wow you must be an old fart since you were born in
    > 1970
    >
    >
    > ?>[/color]

    Look at the eval() function:


    Basically, you'll want to use that on the field's value so that it will
    fill in the correct variables.

    --
    Justin Koivisto - spam@koivi.com
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Comment

    • kaptain kernel

      #3
      Re: Is something like this possible with php?


      look up the "eval" command

      eval ($someField);

      $someField would be:

      print "Welcome back $fname ! wow you must be an old fart since
      you were born in $bday";


      [color=blue]
      >dan wrote:[/color]
      [color=blue]
      > Hi,
      >
      > I am trying to see if this is possible. I have thought about it and
      > cant see an easy way to do this.
      >
      >
      > OK say I have a script
      >
      > <?php
      >
      > //these variables get set in the script
      >
      > $fname='Moe';
      > $bday='1970';
      >
      > //extract a field from mysql
      > //select someField from someTable limit 1;
      > //the variable someField contains this value
      >
      > //$someField='Wel come back $fname ! wow you must be an old fart since
      > you were born in $bday'
      >
      > As you can see the variable is stored in the mysql table with embedded
      > variables in the string itself. What I would like to do is have these
      > variables evaluated somehow so that the $someField value contains this
      > value
      >
      > Welcome back Moe ! wow you must be an old fart since you were born in
      > 1970
      >
      >
      > ?>[/color]

      Comment

      • Tom Thackrey

        #4
        Re: Is something like this possible with php?


        On 17-Nov-2003, basement_addict @yahoo.com (dan) wrote:
        [color=blue]
        > $fname='Moe';
        > $bday='1970';
        >
        > //extract a field from mysql
        > //select someField from someTable limit 1;
        > //the variable someField contains this value
        >
        > //$someField='Wel come back $fname ! wow you must be an old fart since
        > you were born in $bday'
        >
        > As you can see the variable is stored in the mysql table with embedded
        > variables in the string itself. What I would like to do is have these
        > variables evaluated somehow so that the $someField value contains this
        > value[/color]

        eval('$someFiel d="'.$someField .'";');

        --
        Tom Thackrey

        tom (at) creative (dash) light (dot) com
        do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)

        Comment

        • Disco Plumber

          #5
          Re: Is something like this possible with php?

          dan (34.235% quality rating):[color=blue]
          >
          > As you can see the variable is stored in the mysql table with embedded
          > variables in the string itself. What I would like to do is have these
          > variables evaluated somehow so that the $someField value contains this[/color]



          eval is your friend.

          /joe
          --
          Travis Thatcher's case from Bednarz! 3.0 is muddled? In the emo garage,
          Housing's desktop is ice-cold.

          Comment

          • Alvaro G Vicario

            #6
            Re: Is something like this possible with php?

            *** dan wrote/escribió (17 Nov 2003 09:25:12 -0800):[color=blue]
            > //$someField='Wel come back $fname ! wow you must be an old fart since
            > you were born in $bday'
            >
            > As you can see the variable is stored in the mysql table with embedded
            > variables in the string itself. What I would like to do is have these
            > variables evaluated somehow so that the $someField value contains this
            > value[/color]

            I can think of several methods. The easiest one might be this:




            --
            --
            -- Álvaro G. Vicario - Burgos, Spain
            --

            Comment

            • Matthew Crouch

              #7
              Re: Is something like this possible with php?

              Is that a record -- 5 pretty much identical responses?

              Matthew "eval me all night long" Crouch

              "Alvaro G Vicario" <alvaro_QUITAR_ REMOVE@telecomp uteronline.com> wrote in
              message news:x46xsioayh mo.1lqz4hk4ct2o i.dlg@40tude.ne t...[color=blue]
              > *** dan wrote/escribió (17 Nov 2003 09:25:12 -0800):[color=green]
              > > //$someField='Wel come back $fname ! wow you must be an old fart since
              > > you were born in $bday'
              > >
              > > As you can see the variable is stored in the mysql table with embedded
              > > variables in the string itself. What I would like to do is have these
              > > variables evaluated somehow so that the $someField value contains this
              > > value[/color]
              >
              > I can think of several methods. The easiest one might be this:
              >
              > www.php.net/eval
              >
              >
              > --
              > --
              > -- Álvaro G. Vicario - Burgos, Spain
              > --[/color]


              Comment

              • Pedro Graca

                #8
                Re: Is something like this possible with php?

                dan wrote:[color=blue]
                > Hi,
                >
                > $fname='Moe';
                > $bday='1970';[/color]

                single quotes do not interpolate; double quotes do!

                <?php
                $fname = 'Moe';
                $bday = '1970';


                $single_quoted_ Field = 'Hi $fname, $bday was long ago.';

                $double_quoted_ Field = "Hi $fname, $bday was long ago.";

                $unquoted_Field = 'Hi ' . $fname . ', ' . $bday . ' was long ago.';

                $heredoc_Field = <<<EOF
                Hi $fname,
                $bday was long ago.
                EOF;


                echo $single_quoted_ Field; // prints Hi $fname, ...
                echo $double_quoted_ Field; // prints Hi Moe, ...
                echo $unquoted_Field ; // prints Hi Moe, ...
                echo $heredoc_Field; // prints Hi Moe,<newline>.. .
                ?>

                there is no need to eval().

                check


                --
                ..sig

                Comment

                • Disco Plumber

                  #9
                  Re: Is something like this possible with php?

                  Pedro Graca (53.519% quality rating):[color=blue]
                  >
                  > there is no need to eval().[/color]

                  I think you misunderstood the original post. He was not saying he
                  manually assigned that string in PHP. It was stored in a DB field.

                  /joe
                  --
                  In the College of Computing, the gombizler is optimized. Bednarz! 3.0 is
                  hoppy. The syphilis is exciting.

                  Comment

                  • Pedro Graca

                    #10
                    Re: Is something like this possible with php?

                    Disco Plumber wrote:[color=blue]
                    > Pedro Graca (53.519% quality rating):[color=green]
                    >>
                    >> there is no need to eval().[/color]
                    >
                    > I think you misunderstood the original post. He was not saying he
                    > manually assigned that string in PHP. It was stored in a DB field.[/color]

                    Ah! I'm terribly sorry. You're quite right.

                    Maybe the OP can get the data out of the database with the $fname and
                    $bday thing already in there?

                    something like:

                    $sql = "select concat('Hi ', fname, ', ', bday, ' was long ago.')"
                    . " from table"
                    . " where id between 4 and 7";

                    Of course he'd have to reestructure the database and that may not be a
                    good idea :)

                    --
                    ..sig

                    Comment

                    • Jeffrey Silverman

                      #11
                      Re: Is something like this possible with php?

                      On Mon, 17 Nov 2003 09:25:12 -0800, dan wrote:
                      [color=blue]
                      > Welcome back Moe ! wow you must be an old fart since you were born in
                      > 1970[/color]

                      Hey! I was born in 1970! I'm not *that* old!

                      --
                      Jeffrey D. Silverman | jeffrey AT jhu DOT edu
                      Johns Hopkins University | Baltimore, MD
                      Website | http://www.wse.jhu.edu/newtnotes/

                      Comment

                      • dan

                        #12
                        Re: Is something like this possible with php?

                        basement_addict @yahoo.com (dan) wrote in message news:<744d583c. 0311170925.166d a7e1@posting.go ogle.com>...[color=blue]
                        > Hi,
                        >
                        > I am trying to see if this is possible. I have thought about it and
                        > cant see an easy way to do this.[/color]

                        <SNIP>



                        Thanks everyone.

                        I thought it was eval() that would solve it but wasn't sure of the syntax.

                        It makes sense now.

                        Comment

                        • dan

                          #13
                          Re: Is something like this possible with php?

                          basement_addict @yahoo.com (dan) wrote in message news:<744d583c. 0311170925.166d a7e1@posting.go ogle.com>...[color=blue]
                          > Hi,
                          >[/color]


                          I have another question regarding this.


                          Lets say, for example, the text that is being eval()'d contains the
                          string:


                          "Welcome to $_SERVER['DOCUMENT_ROOT'] you old fart!"


                          When I eval() this string I get an error like this.

                          PHP Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or
                          `T_NUM_STRING' in /home/...

                          Is there a way around this?


                          Thanks

                          Comment

                          • Pedro Graca

                            #14
                            Re: Is something like this possible with php?

                            dan wrote:[color=blue]
                            > [...]
                            > "Welcome to $_SERVER['DOCUMENT_ROOT'] you old fart!"
                            >
                            >
                            > When I eval() this string I get an error like this.
                            >
                            > PHP Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or
                            > `T_NUM_STRING' in /home/...
                            >
                            > Is there a way around this?[/color]

                            The single quotes are messing up the evaluation.

                            I tried

                            ---- 8< ---- cut here --------
                            <?php
                            $x1 = '$y1 = "Welcome $_SERVER[\'PHP_SELF\'], good-night!";';
                            $x2 = '$y2 = "Welcome ${_SERVER[\'PHP_SELF\']}, good-night!";';
                            $x3 = '$y3 = "Welcome $_SERVER[PHP_SELF], good-night!";';

                            # $x2 ==> add { } around the full variable
                            # $x3 ==> remove single quotes completely

                            echo "\n\n-- 1 ------\n";
                            echo "evaluating [$x1] ...\n";
                            $z1 = eval($x1);
                            echo 'y1 = ', $y1, "\n";
                            echo 'z1 = ', $z1, "\n";

                            echo "\n\n-- 2 ------\n";
                            echo "evaluating [$x2] ...\n";
                            $z2 = eval($x2);
                            echo 'y2 = ', $y2, "\n";
                            echo 'z2 = ', $z2, "\n";

                            echo "\n\n-- 3 ------\n";
                            echo "evaluating [$x3] ...\n";
                            $z3 = eval($x3);
                            echo 'y3 = ', $y3, "\n";
                            echo 'z3 = ', $z3, "\n";
                            ?>
                            ---- 8< ---- cut here --------

                            HTH

                            --
                            ..sig

                            Comment

                            • Tom Thackrey

                              #15
                              Re: Is something like this possible with php?


                              On 18-Nov-2003, basement_addict @yahoo.com (dan) wrote:
                              [color=blue]
                              > I have another question regarding this.
                              >
                              >
                              > Lets say, for example, the text that is being eval()'d contains the
                              > string:
                              >
                              >
                              > "Welcome to $_SERVER['DOCUMENT_ROOT'] you old fart!"
                              >
                              >
                              > When I eval() this string I get an error like this.
                              >
                              > PHP Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or
                              > `T_NUM_STRING' in /home/...
                              >
                              > Is there a way around this?[/color]

                              Your string has incorrect syntax, it should be:
                              "Welcome to $_SERVER[DOCUMENT_ROOT] you old fart!"

                              further eval() does not return a value unless you include a return in the
                              eval code. You probably want something like:

                              eval('$str = "Welcome to $_SERVER[DOCUMENT_ROOT] you old fart!";');
                              echo $str;



                              --
                              Tom Thackrey

                              tom (at) creative (dash) light (dot) com
                              do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)

                              Comment

                              Working...