delete the values in the dropdown if the user selected values?

if the user has selected 2 in the 1st dropdown then 2 should be deleted from the other 4 dropdowns like this.
<?php 
$max=5;
for($a=1;$a<=5;$a++)
{
?>
	<select name="<?php echo 't'.$a; ?>">
	<?php
		for($i=1;$i<=$max;$i++){
	?>
		<option <?php if($i==$a){?> selected="selected" <?php }  else { }?> >
	<?php
		echo $i;
	?>
		</option>
<?php 
}
?>
	</select>		
}
?>

Open in new window

designersxAsked:
Who is Participating?
 
Vimal DMConnect With a Mentor Senior Software EngineerCommented:
hi,

follow the code which i have pasted

and try the thing send the feed back of it

i have done it for one list box u can just make for four list boxs as u said

<?php
$max=5;
for($a=1;$a<=5;$a++)
{
?>
        <select name="<?php echo 't'.$a; ?>">
        <?php
                for($i=1;$i<=$max;$i++){
        ?>
                <option <?php if($i==$a){?> selected="selected" <?php }  else { }?> >
        <?php
                echo $i;
        ?>
                </option>
<?php
}
?>
        </select>              



submit the form and get the selected value and set as common value

consider the selected value is $cmbValue = 2

<?php
$max=5;
for($a=1;$a<=5;$a++)
{
?>
        <select name="<?php echo 't'.$a; ?>">
        <?php
                for($i=1;$i<=$max;$i++){
                        if($i!=$cmbValue){
        ?>
                <option <?php if($i==$a){?> selected="selected" <?php }  else { }?> >
        <?php
                  }
                echo $i;
        ?>
                </option>
<?php
}
?>
        </select>              
0
 
TaplarCommented:
The following should, given that $selectOneValue would be the variable containing the selected value off of the first selection list which I would assume would come in from a form submit, remove the option from the proceeding selection lists.  It simply checks to see if it is generating a selection list beyond the first one and if so, if the option it is trying to create on that pass is the same as the submitted value for selection list one, continue past it.
<? $selectOneValue = 2;
   $max = 5;
 
   for ($a = 1; $a <= 5; $a++) { ?>
<select name="t<?= $a ?>">
<?   for ($i = 1; $i <= $max; $i++) {
       if ($a > 1 && $i == $selectOneValue) {
         continue;
       } ?>
 <option value="<?= $i ?>"
<?     if ($i == $a) { ?>
  selected="selected"
<?     } ?>><?= $i ?></option>
<?   } ?>
</select>
<? } ?>

Open in new window

0
 
designersxAuthor Commented:
i am sorry Taplar: sir i am unable to understand what u did, can u please take an static example and then tell me how u are proceeding.

otherwise thanks a lot for ur code.
0
 
TaplarCommented:
I'll step through it.

$selectOneValue is the variable that contains the value you want to select from the first selection list and not include in your proceeding selection lists.  In your working example it may actually be a variable like $_POST['t1'] .  I just used selectOneValue for an example.

$max = 5 as the number of selection lists to create.

We then go into the for loop to create the selection lists.  We first print out the selection with a name of "t<? $a =?>" .   <?= $variable ?> is the equivalent of <? echo $variable; ?> .  It's short hand in a way.

We then go into a for loop to print out the selection options.  The first thing we then check is to see if $a > 1 && $i == $selectOneValue.  This provides the conditional that is only happens if we are not generating the first selection box and the option we're trying to create for the selection is the value we want to not include.  If both these conditions are met, we are creating a selection list that is not the first one and we are trying to create an option that was selected in the first selection list.  To delete it, or skip its generation, we use continue; .   continue; is a command used in conjunction with loops.  It makes the loop skip past all proceeding code in the loop and go back to the start of the loop.  So continue; lets us skip the option generation and thus, deleting it from all the proceeding selections other than the first one.

We then have the printing out of the option and selecting of a default one like your original code did, and then the closing of the selection lists.


<? $selectOneValue = 2;
   $max = 5;
 
   for ($a = 1; $a <= 5; $a++) { ?>
<select name="t<?= $a ?>">
<?   for ($i = 1; $i <= $max; $i++) {
       if ($a > 1 && $i == $selectOneValue) {
         continue;
       } ?>
 <option value="<?= $i ?>"
<?     if ($i == $a) { ?>
  selected="selected"
<?     } ?>><?= $i ?></option>
<?   } ?>
</select>
<? } ?>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.