Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Dynamically change a variables name

  1. #1
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Dynamically change a variables name

    Is it possible to change the name of the variable dynamically

    e.g.

    I want to set these variables on a page:
    PHP Code:
    $id1 $_GET['id1'];
    $id2 $_GET['id2'];
    $id3 $_GET['id3'];
    $id4 $_GET['id4'];
    etc... 
    only I don't know how many of these I will have on the page as the number will be generated from the previous page. could be 4 or 100.

    I think i need some kind of loop such as
    PHP Code:
    for ($i=1$i<=$qty$i++)
    {
    $id* = $_GET['id*'];

    I want to change $id* to $id1, $id2 ,$id3, $id4 etc...
    depending on the value of $qty

    does anyone know if this is even possible? Or if i'm making any sense!

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    PHP Code:
    for ($i=1$i<=$qty$i++)
    {
    $id.$i $_GET['id'.$i];


  3. #3
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    Sweet! easy as that.

    Thanks!

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ah, I wasn't aware that was valid syntax. Thanks for that, boogyman.

    The method I have always used is "variable variables", like this:

    $$name = 1;

    So....
    $name = 'test';
    $$name = 1;
    echo $test; //1

    If you need to have a more complex variable name, you'll just need to create it in the previous line:

    $name = $a.'b'.$c.'abc';
    $$name; //use as you want


    Though $a.$b may work, be cautious when modifying that syntax as some forms do not work. $$var will always work, and refer to the variable named the value of $var.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by djr33 View Post
    Ah, I wasn't aware that was valid syntax. Thanks for that, boogyman.

    The method I have always used is "variable variables", like this:

    $$name = 1;

    So....
    $name = 'test';
    $$name = 1;
    echo $test; //1

    If you need to have a more complex variable name, you'll just need to create it in the previous line:

    $name = $a.'b'.$c.'abc';
    $$name; //use as you want


    Though $a.$b may work, be cautious when modifying that syntax as some forms do not work. $$var will always work, and refer to the variable named the value of $var.
    I have never had any problems with doing it that way. Whether its technically valid or not you would probably have to refer to either guru that is Twey / John.
    I can understand your method of assigning the variable by reference, but personally concantenating the variables are easily for me.. well at least in this instance. Its really no different than if you were doing

    Code:
    for($i=0; $i<max; $i++)
    {
          $var[$i] = something;
    }

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    $id.$i = $_GET['id'.$i];
    This will not work, at least in PHP5. Instead, it will be interpreted as $id . ($i = $_GET['id' . $i]). djr33's method is the correct one, but any use of variable variables is almost certainly an indicator of bad code. The simplest way of accomplishing what you want is:
    Code:
    foreach($_GET as $k => $v)
      if(strpos($k, 'id') === 0)
        $$k = $v;
    However, I would strongly suggest:
    • Not doing this; this style of coding
      • opens up several security risks for which you must plan if you intend to use it;
      • clutters up the global namespace;
      • will not allow autoglobal access to these variables inside functions, so you'd have to do
        Code:
        function foo() {
          global $id1;
          global $id2;
          global $id3;
          global $id4;
        }
        ... or equivalent inside your functions.
      If you find $_GET['id4'] too much to type, you can always copy $_GET to another variable:
      Code:
      $g = $_GET;
    • Using an array for these values; according to the CGI specification simply having multiple elements in your form with the same name should be enough to cause the value to be an array as handled in your server-side script, but PHP is somewhat broken in respect to this specification, and therefore when passing values to it in an array you must end the names with []:
      Code:
      <input type="text" name="ids[]">
      <input type="text" name="ids[]">
      <input type="text" name="ids[]">
      <!-- ... -->
      $_GET['ids'] will then be an array containing all these values, which you can manipulate using normal array functions.
    Whether its technically valid or not you would probably have to refer to either guru that is Twey / John.
    As far as I'm aware, John knows no PHP. I envy him this somewhat
    Its really no different than if you were doing [an array assignment]
    It is very different, primarily in that the syntax is ambiguous (and apparently differs between versions) and also in that it relies upon a deprecated feature, namely the interpretation of id as a string rather than an undefined constant, to work.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    Quote Originally Posted by Twey View Post
    Using an array for these values; according to the CGI specification simply having multiple elements in your form with the same name should be enough to cause the value to be an array as handled in your server-side script, but PHP is somewhat broken in respect to this specification, and therefore when passing values to it in an array you must end the names with []:
    Code:
    <input type="text" name="ids[]">
    <input type="text" name="ids[]">
    <input type="text" name="ids[]">
    <!-- ... -->
    $_GET['ids'] will then be an array containing all these values
    This was exactly what i was looking to do, thanks!
    Looked as though I was trying to do it the hard way!

    Following on from that though, I have now collected two differant arrays using this method

    e.g.
    $id = array( [0] => 100 [1] => 221 [2] => 343 );
    and
    $qty = array( [0] => 3 [1] => 6 [2] => 1 );

    Am i able to merge them?
    I need to end up with an array like this:

    $cart = array(100,100,100,221,221,221,221,221,221,343);

    Notice that the first value "100" matches up with the "3" and prints "100,100,100" and "221" to the "6" and so on...
    Last edited by jc_gmk; 10-26-2007 at 08:25 AM.

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $cart = array();
    foreach($id as $k => $v)
      for($i = 0; $i < $qty[$k]; ++$i)
        $cart[] = $v;
    Last edited by Twey; 10-26-2007 at 09:34 AM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    There are also simple functions, like array_merge(), but I'm not sure that's customizable enough for your current purposes.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    No luck, if the first value of $id is "100" and $qty is "6" it just prints out this array:

    Array ( [0] => 600 [1] => 0 [2] => 0 [3...

    i would need it to look like this:

    Array ('100','100','100','100','100','100')

Bookmarks

Posting Permissions

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