Dynamic Variable Names

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael J. Astrauskas

    Dynamic Variable Names

    I have a shopping cart page that allows a user to type in a new
    quantity and press a [Submit] button. The quantities entered are in form
    text boxes with the names "item302", "item7812", etc. reflecting which
    item that particular textbox is associated to. I store the entered
    variables using 'import_request _variables("p", "p_")' so I end up with
    $item302, $item7812, etc. My script looks for these variables by looking
    through the shopping cart array which stores the item numbers themselves
    and then checks the value of $item<whatever> .

    If I have a number stored in $item is there any easy way to tell PHP
    to check the value of variable $p_item<item>?

    For example, if $item is '302' how can I tell PHP to evaluate $p_item302?

    Thank you!

    --
    - Michael J. Astrauskas

    P.S. I'm currently doing this using the eval() function, but I don't
    like that technique.

  • 127.0.0.1

    #2
    Re: Dynamic Variable Names

    Michael J. Astrauskas wrote:
    [color=blue]
    > For example, if $item is '302' how can I tell PHP to evaluate
    > $p_item302?[/color]

    Try:

    $varname = sprintf("p_item %d", $item);
    echo($$varname)

    --
    Spam:newsgroup( at)craznar.com@ verisign-sux-klj.com
    EMail:<01100011 001011100110001 001110101011100 10011010110
    110010101000000 011000110111001 001100001011110 10011011100
    110000101110010 001011100110001 101101111011011 0100100000>

    Comment

    • Timo Henke

      #3
      Re: Dynamic Variable Names

      127.0.0.1 wrote:[color=blue]
      > Michael J. Astrauskas wrote:
      >[color=green]
      >> For example, if $item is '302' how can I tell PHP to evaluate
      >> $p_item302?[/color]
      >[/color]

      Try to define a dynamic Var:

      $item = "ABC";

      ${"p_item{$item }"} = "value of this var";

      print $p_itemABC;

      would output "value of this var";

      greetings .. timo

      Comment

      • Nikolai Chuvakhin

        #4
        Re: Dynamic Variable Names

        "Michael J. Astrauskas" <trevie@cox.net > wrote in message
        news:<himfb.463 12$vj2.21136@fe d1read06>...[color=blue]
        >
        > if $item is '302' how can I tell PHP to evaluate $p_item302?[/color]

        Use a variable variable:

        $name = 'p_item' . $item;
        $value = $$name;

        Since $name evaluates to 'p_item302', $$name must evaluate
        to $p_item302. See the Manual for details:



        Cheers,
        NC

        Comment

        • Michael J. Astrauskas

          #5
          Re: Dynamic Variable Names

          Nikolai Chuvakhin wrote:
          [color=blue]
          > Michael J. Astrauskas" wrote:
          >[color=green]
          >> if $item is '302' how can I tell PHP to evaluate $p_item302?[/color]
          >
          > Use a variable variable:
          >
          > $name = 'p_item' . $item;
          > $value = $$name;[/color]

          Thank you! I combined what you and Timo Henke suggested and it works
          beautifully!

          Here is my final code:
          $v = ${'p_ITEM'.$key };

          --
          - Michael J. Astrauskas

          Comment

          Working...