user2466952
user2466952

Reputation: 196

PHP while, foreach drop down menu the same selected value in all drop dow menus

3 drop down menus with values

$options1 = array( 1=>'= Equals', '≠ Does not Equal', '> Is greater than', '≥ Is greater than or equal to', '< Is less than', '≤ Is less than or equal', '∋ Contains', '∌ Does not contain');

$counter_maximum = 3;

while ($counter < $counter_maximum){

echo '
<select name="entry_id_selector[]">';
foreach ( $options1 as $i1=>$opt1 ) :
echo '<option value="' .$i1 .'"';

foreach ($entry_id_selector_topic as $entry_id_selector_i=>$entry_id_selector_topic_value ) :
echo (($i1 == $entry_id_selector_topic_value)? 'selected' : "");
endforeach;

echo '>';
echo $opt1 .'</option>';
endforeach;
echo '</select>';

$counter++;
}

User selects some value

Set variables (array) with user`s selected values (this code is above all other code; is at top of page)

$entry_id_selector_topic = $_POST['entry_id_selector'];

Default value (after page first loads) is the first value from $options1 or = Equals. After user clicks Submit button I want to remember user`s selected values.

But with this code is folowing behavior:

1) If user changes/select value only in one drop down menu, then after click on Submit, values in all drop down menus change to user`s selected value.

2) If user changes values in more than one drop down menu then values in all menus changes/selected to value of the last drop down menu where value is changed.

When experimenting tried to check $entry_id_selector_topic_value and just below $entry_id_selector_topic = $_POST['entry_id_selector']; placed this code

foreach ($entry_id_selector_topic as $entry_id_selector_i=>$entry_id_selector_topic_value ) {
echo $entry_id_selector_topic_value .' $entry_id_selector_topic_value<br>';
}

Here $entry_id_selector_topic_value is exactly the value that is chosen from each drop down menu.

What need to correct (change) to remember user`s selected value in each dropdown menu?

Upvotes: 3

Views: 694

Answers (2)

user2465936
user2465936

Reputation: 1040

This is working code

$counter = 0;
$counter_maximum = 3;

while ($counter < $counter_maximum){
$entry_id_selector_topic = $_POST['entry_id_selector'][$counter];

echo '
<select name="entry_id_selector[]">';
foreach ( $options1 as $i1=>$opt1 ) :
echo '<option value="' .$i1 .'"';

echo (($i1 == $entry_id_selector_topic)? 'selected' : "");

echo '>';
echo $opt1 .'</option>';
endforeach;
echo '</select>';

$counter++;

Upvotes: 3

foreach ($entry_id_selector_topic as $entry_id_selector_i=>$entry_id_selector_topic_value ) :
echo (($i1 == $entry_id_selector_topic_value)? 'selected' : "");
endforeach;

This part is not correct, your name should be

<select name="entry_id_selector[$counter]">

you shouldn add another array if multiple values are impossible, then you should do so

if ($entry_id_selector_topic[$counter] == $i) echo ' selected ';

Giving, such a long names its not preferable. Also you can write

foreach ($entry_id_selector_topic as $entry_id_selector_topic_value ) :

Upvotes: 0

Related Questions