PeggyBuckham Posted September 7, 2012 Share Posted September 7, 2012 I have post data that I want to get information about using an array...I want to add the counter to a variable get the value...this example might make more sense. No this example does not work. for($i=1;$i<=count($example_array);$i++){ if( $variable_name_.$i == $example_array[$i]){ echo ' checked="checked" '; } } what i am trying to do is get the value for the variables: $variable_name_1 , $variable_name_2 , $variable_name_3 , $variable_name_4 etc. Hope this makes sense! Quote Link to comment https://forums.phpfreaks.com/topic/268138-get-value-for-variable-by-adding-a-counter-as-part-of-the-name/ Share on other sites More sharing options...
requinix Posted September 8, 2012 Share Posted September 8, 2012 Don't. Use an array instead. Quote Link to comment https://forums.phpfreaks.com/topic/268138-get-value-for-variable-by-adding-a-counter-as-part-of-the-name/#findComment-1376214 Share on other sites More sharing options...
Christian F. Posted September 8, 2012 Share Posted September 8, 2012 Use arrays, not sequentially named variables. Variable variables, which is what you're trying to use here, is a very bad idea! As you're seeing already. Not only do they make things harder to solve as you're writing the code, but they'll make things a lot harder to maintain after a few weeks of not looking at the code. Quote Link to comment https://forums.phpfreaks.com/topic/268138-get-value-for-variable-by-adding-a-counter-as-part-of-the-name/#findComment-1376215 Share on other sites More sharing options...
PeggyBuckham Posted September 10, 2012 Author Share Posted September 10, 2012 The point of the array in the first place is that I want to use the names of the variables...fields in more than one place and I only want to to change it in one place. Quote Link to comment https://forums.phpfreaks.com/topic/268138-get-value-for-variable-by-adding-a-counter-as-part-of-the-name/#findComment-1376566 Share on other sites More sharing options...
PeggyBuckham Posted September 10, 2012 Author Share Posted September 10, 2012 Um I guess I did not explain that very well. I will post more of the code tomorrow so that it makes more sense. I don't have the code here today Quote Link to comment https://forums.phpfreaks.com/topic/268138-get-value-for-variable-by-adding-a-counter-as-part-of-the-name/#findComment-1376586 Share on other sites More sharing options...
Christian F. Posted September 10, 2012 Share Posted September 10, 2012 Sounds like a good idea, yeah. I didn't quite understand what you meant with the previous post, except that you apparently want to re-use some code. Adding some more information, and explaining the specifics of your end goal, should help out quite a bit. Quote Link to comment https://forums.phpfreaks.com/topic/268138-get-value-for-variable-by-adding-a-counter-as-part-of-the-name/#findComment-1376588 Share on other sites More sharing options...
PeggyBuckham Posted September 10, 2012 Author Share Posted September 10, 2012 Ok this should make more sense. I have an array that contains the values of checkboxes. I have used the array as part of a form. each checkbox has a different name...I know that is unusual, but i want to put the value(s) into separate fields of a database. I'm trying to get the value of the variables; $example_1 , $example_2 , $example_3. I am going to use this array later to read back the information that was submitted. I thought I was being clever so that in the future it would be easy to add to or edit the checkbox values. But I overlooked the fact that I did not know how do get the values of $example_1 , $example_2 etc. I will keep playing with it for awhile. I'm sure there is an answer. <?php //-------------------------------------------------------------------------make variable out of Post value foreach ($_POST as $key => $value) { $$key = $value; } $c = 1; $example_array = array( $c++ => 'Blue', $c++ => 'Green', $c++ => 'Yellow', ); //----------------------------------------------------------------------------this is part of a form for($i=1;$i<=count($example_array);$i++){ echo '<p><input style="width: 15px; margin:0" type="checkbox" name="example_'.$i.'" value="'.$example_array[$i].'"'; if('example_'.$i == $example_array[$i]){ echo ' checked="checked" '; } echo '/> '.$example_array[$i].'</p>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/268138-get-value-for-variable-by-adding-a-counter-as-part-of-the-name/#findComment-1376719 Share on other sites More sharing options...
PeggyBuckham Posted September 10, 2012 Author Share Posted September 10, 2012 Ok this works. It just seems like I took something that should have been simple and turned it into something complicated. i will leave this open for awhile in case someone has any suggestions on how i could have made it simpler. But this worked. I just added another array. <?php //-------------------------------------------------------------------------make variable out of Post value foreach ($_POST as $key => $value) { $$key = $value; } $c = 1; $example_array = array( $c++ => 'Blue', $c++ => 'Green', $c++ => 'Yellow', ); $c = 1; $variable_array = array( $c++ => 'example_1', $c++ => 'example_2', $c++ => 'example_3', ); //----------------------------------------------------------------------------this is part of a form for($i=1;$i<=count($example_array);$i++){ echo '<p><input style="width: 15px; margin:0" type="checkbox" name="example_'.$i.'" value="'.$example_array[$i].'"'; if($$variable_array[$i] == $example_array[$i]){ echo ' checked="checked" '; } echo '/> '.$example_array[$i].'</p>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/268138-get-value-for-variable-by-adding-a-counter-as-part-of-the-name/#findComment-1376729 Share on other sites More sharing options...
Psycho Posted September 10, 2012 Share Posted September 10, 2012 Ok this works. It just seems like I took something that should have been simple and turned it into something complicated. i will leave this open for awhile in case someone has any suggestions on how i could have made it simpler. But this worked. I just added another array. Yes, it is complicated because you are trying to create variables where none are needed. This is a complete waste: foreach ($_POST as $key => $value) { $$key = $value; } So, if there is a field called 'foo' in your form you are creating a variable called $foo in your code. Why? Just reference $_POST['foo']. You are creating a whole extra level of complexity where none is needed. Also, I'm not sure from your code if the two arrays of values for the two options are used elsewhere or not. E.g. do you have those in the database? If so, you should query the database for the IDs and values. If not, then you should hard code the arrays and include them on the pages where they are needed. I wouldn't create them dynamically as you have done. Making changes could cause previous entries to report the wrong results. So, looking through your code it appears you have some groups of checkboxes and you want to auto-populate one in each group based upon the submitted values. You should create a function that takes two parameters: the array of options and the selected value. I'm not following everything you are doing, but the code below should get you started. To use it you only need to define the name/value pairs to use for each group of checkboxes and provide a "name" for the checkboxes. The name will be used to create an array of checkboxes. Also, the name is used to check for an existing POST values of the same name to use for the purposes of selecting the appropriate selected checkboxes. This is a complete working example <?php //These should be pulled from the DB $color_array = array( 1 => 'Blue', 2 => 'Green', 3 => 'Yellow', ); $example_array = array( 1 => 'example_1', 2 => 'example_2', 3 => 'example_3', ); function createCheckboxGroup($name, $valuesAry) { $checkboxesHTML = ''; //Check POST data to see if there was a selected value $selectedValues = (isset($_POST[$name])) ? $_POST[$name] : array(); //Create the checkbox group foreach($valuesAry as $value => $label) { $checked = (in_array($value, $selectedValues)) ? ' checked="checked"' : ''; $checkboxesHTML .= "<p><input style='width: 15px; margin:0' type='checkbox' name='{$name}[]' value='{$value}'{$checked} />{$label}</p>\n"; } return $checkboxesHTML; } ?> <html> <body> <form action="" method="post"> <b>Select your colors:</b><br> <?php echo createCheckboxGroup('color', $color_array); ?> <b>Select your examples:</b><br> <?php echo createCheckboxGroup('example', $example_array); ?> <br><button type="submit">Submit</submit> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/268138-get-value-for-variable-by-adding-a-counter-as-part-of-the-name/#findComment-1376738 Share on other sites More sharing options...
PeggyBuckham Posted September 11, 2012 Author Share Posted September 11, 2012 Thank You! Actually i will study this and figure out all of the syntax you are using. Some is unfamiliar to me...but i need to learn. So Thank You! Quote Link to comment https://forums.phpfreaks.com/topic/268138-get-value-for-variable-by-adding-a-counter-as-part-of-the-name/#findComment-1377066 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.