Dynamically Changing the Name of a Variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vigile
    Senior Member
    • Oct 2000
    • 376

    Dynamically Changing the Name of a Variable

    Lets say I have a POST sending along an unknown number of check box variables, and in the next PHP doc in the submit URL, I want to do somethign with each of those values. I have conveniently used a loop in PHP to create the names of hte check boxes: Check1 - Check8 (but it could later be changed from Check1-CHeck11, but I don't want to have to alter this code).

    In the submit.php code, I want to be able to cycle through all of these check boxes. I have a way to loop through the total number of check boxs that were there, but I don't know how to dynamicly create each variable name to test: ie I want to do:

    Is Check1 = 1?
    Is Check2 = 1?
    Is Check3 = 1?

    But I cant figure out how to change the 1/2/3 part of the variable name as I go.

    Is there anything like:

    for ($i=0; $i<checktotal; $i++) {
    if ($Check$i ==1)
    do something
    }

    Thanks!
  • Cyborg from DH
    Senior Member
    • Nov 2002
    • 305

    #2
    or send all the checkboxes along with an array.

    Comment

    • Zoombie
      New Member
      • Mar 2002
      • 18

      #3
      PHP Code:
      for ($i=0$i<maxnumberofchecks$i++) {
        
      $var="Check$i";
        if(isset($
      $var)){
          if ($
      $var==1)
            
      // do something
          
      }
        }else{
          break;
        }

      It looks like you're working with register_globals set to on, though...the recommendation would be to learn how to work without register_globals, as it makes it extremely easy to leave loopholes in your code.

      Comment

      • Vigile
        Senior Member
        • Oct 2000
        • 376

        #4
        Thanks!

        Comment

        • Gamefreak
          New Member
          • Feb 2002
          • 19

          #5
          Zoombie, I don't think you can use $$var to represent the variable.
          Code:
          for ([color=#0000cc]$i
           
          Wouldn't you have to use:
           
          [color=#006600]=[/color][color=#0000cc]0[/color][color=#006600]; [/color][color=#0000cc]$i[/color][color=#006600]<[/color][color=#0000cc]maxnumberofchecks[/color][color=#006600]; [/color][color=#0000cc]$i[/color][color=#006600]++) { 
            [/color][color=#0000cc]$var[/color][color=#006600]=[/color][color=#cc0000]"\$Check$i"[/color][color=#006600]; 
            if(isset(eval($var)[/color][color=#006600])){ [/color]
          [color=#006600]	if (eval($var)[/color][color=#006600]==[/color][color=#0000cc]1[/color][color=#006600]) 
          	  [/color][color=#ff9900]// do something 
          	[/color][color=#006600]} 
            }else{ 
          	break; 
            } 
          } [/color]
          [/color]

          Comment

          • MUG
            Senior Member
            • Apr 2002
            • 1191
            • 2.3.0

            #6
            Originally posted by Gamefreak
            Zoombie, I don't think you can use $$var to represent the variable.
            Code:
            for ([color=#0000cc]$i
             
            Wouldn't you have to use:
             
            [color=#006600]=[/color][color=#0000cc]0[/color][color=#006600]; [/color][color=#0000cc]$i[/color][color=#006600]<[/color][color=#0000cc]maxnumberofchecks[/color][color=#006600]; [/color][color=#0000cc]$i[/color][color=#006600]++) { 
              [/color][color=#0000cc]$var[/color][color=#006600]=[/color][color=#cc0000]"\$Check$i"[/color][color=#006600]; 
              if(isset(eval($var)[/color][color=#006600])){ [/color]
            [color=#006600]	if (eval($var)[/color][color=#006600]==[/color][color=#0000cc]1[/color][color=#006600]) 
            	  [/color][color=#ff9900]// do something 
            	[/color][color=#006600]} 
              }else{ 
            	break; 
              } 
            } [/color]
            [/color]
            You can use $$var. That is called a 'variable variable'.

            However your example will not work.

            Comment

            • Cyborg from DH
              Senior Member
              • Nov 2002
              • 305

              #7
              you can also have string named variables:

              ${"hello this is a variable"}

              Comment

              • Gamefreak
                New Member
                • Feb 2002
                • 19

                #8
                Cool! I never knew that before. Whaddya know? I learned something new while I was trying to help somethone out!

                Comment

                • TeKnIcIaN
                  Member
                  • Feb 2003
                  • 53

                  #9
                  I do something like this for changing the display order of items in my catalog scripts. My post vars are something like "displayorder_1" to "displayorder_50" or whatever.
                  PHP Code:
                  foreach ($HTTP_POST_VARS as $key => $value) {
                      list(
                  $crap$id) = explode("displayorder_"$key);
                      if (
                  $id 0) {
                          
                  // do stuff
                      
                  }

                  Works great for me!

                  Comment

                  • MUG
                    Senior Member
                    • Apr 2002
                    • 1191
                    • 2.3.0

                    #10
                    Originally posted by TeKnIcIaN
                    I do something like this for changing the display order of items in my catalog scripts. My post vars are something like "displayorder_1" to "displayorder_50" or whatever.
                    PHP Code:
                    foreach ($HTTP_POST_VARS as $key => $value) {
                        list(
                    $crap$id) = explode("displayorder_"$key);
                        if (
                    $id 0) {
                            
                    // do stuff
                        
                    }

                    Works great for me!
                    BTW, here is a trick about list():
                    PHP Code:
                    foreach ($HTTP_POST_VARS as $key => $value) {
                        list(,
                    $id) = explode("displayorder_"$key); // you don't have to fill the first argument if you don't want to use that key
                        
                    if ($id 0) {
                            
                    // do stuff
                        
                    }

                    Comment

                    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                    Working...