0

Hi what i would like to do in my dropdown is to put a condition if($formdata[0]['Phase']==1){ to show only the option values from $ahk 1x in dropdown but if($formdata[0]['Phase']==3){ to show only the option values from $ahk 3x in dropdown here is my code

<div class="form-group col-sm-6 col-xs-12">
<label for="d13"><?php echo $t[$tmpvalues[0]['Lang']]['d13'];?></label>
<select class="form-control el-text-box" name="d13" onchange="this.form.submit()">
<?php for ($j=0; $j<sizeof($ahk); $j++){ 
$selected="";if($ahk[$j]==$formdata[0]['d13']){$selected="selected";}
?>
<option value="<?php echo $ahk[$j];?>" <?php echo $selected; ?>><?php echo $ahk[$j];?></option>
<?php 
} 
?>
</select>
</div>

values of $ahk are

$ahk[0]="1X10A";
$ahk[1]="1X20A";
$ahk[2]="1X30A";
$ahk[3]="1X40A";
$ahk[4]="1X50A";
$ahk[5]="1X60A";
$ahk[6]="1X70A";
$ahk[7]="1X80A";
$ahk[8]="3X10A";
$ahk[9]="3X20A";
$ahk[10]="3X30A";
$ahk[11]="3X40A";
$ahk[12]="3X50A";
$ahk[13]="3X60A";
$ahk[14]="3X80A";
$ahk[15]="3X100A";
$ahk[16]="3X160A";
$ahk[17]="3X200A";
$ahk[18]="3X300A";
$ahk[19]="3X400A";
$ahk[20]="3X500A";
$ahk[21]="3X1000A";
$ahk[22]="3X1600A";
$ahk[23]="3X2000A";
$ahk[24]="3X2600A";
$ahk[25]="3X3200A";

1 Answer 1

1

Use an if statement to check if the array value matches the phase.

<div class="form-group col-sm-6 col-xs-12">
<label for="d13"><?php echo $t[$tmpvalues[0]['Lang']]['d13'];?></label>
<select class="form-control el-text-box" name="d13" onchange="this.form.submit()">
<?php
$prefix = $formdata[0]['Phase'] . "X";
foreach ($ahk as $option) {
    if (substr($option, 0, 2) == $prefix) {
        $selected = $option == $formdata[0]['d13'] ? "selected" : "";
        ?>
        <option value="<?php echo $option;?>" <?php echo $selected; ?>><?php echo $option;?></option>
        <?php
    }
}
?>
</select>
</div>
Sign up to request clarification or add additional context in comments.

21 Comments

Can you please combine it in my code as i dont know what to delete?
I've done it, but I think it should be obvious. My foreach loop simply replaces your for loop.
i think i something wrong as its not working, maeby i left something behind on the original code, <?php } ?> <?php if($formdata[0]['Meletes_id']>0){ ?> <!-- <div class="form-group col-lg-4 col-md-6 col-sm-12">--> <div class="form-group col-sm-6 col-xs-12">
page is not loading
There's an extra ). If you don't use an IDE to write your code, you should, it will catch things like this automatically. Unfortunately, I don't use mine when answering on SO.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.