Jump to content

get value for variable by adding a counter as part of the name


PeggyBuckham

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';

}

?>

Link to comment
Share on other sites

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>';

}

?>

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.