Announcement

Collapse
No announcement yet.

Dynamically Changing the Name of a Variable

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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!

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

    Comment


    • #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


      • #4
        Thanks!

        Comment


        • #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


          • #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


            • #7
              you can also have string named variables:

              ${"hello this is a variable"}

              Comment


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

                Comment


                • #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


                  • #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...
                    X