database - advice on tidying up php that is checking DB entry & changing select dropdown 'selected' -
i looking advice , best way tidy following php code.
i checking database $this->item->values[''] , changing select dropdown's selected="selected" accordingly.
how can made cleaner/simpler?
<?php $minbedroomscheck = ''; if ( $this->item->values['min-bedrooms'] < '0' ) { $minbedroomscheck = 'selected="selected"'; } $minbedroomsanycheck = ''; if ( $this->item->values['min-bedrooms'] == '0' ) { $minbedroomsanycheck = 'selected="selected"'; } $minbedrooms1check = ''; if ( $this->item->values['min-bedrooms'] == '1' ) { $minbedrooms1check = 'selected="selected"'; } $minbedrooms2check = ''; if ( $this->item->values['min-bedrooms'] == '2' ) { $minbedrooms2check = 'selected="selected"'; } $minbedrooms3check = ''; if ( $this->item->values['min-bedrooms'] == '3' ) { $minbedrooms3check = 'selected="selected"'; } $minbedrooms4check = ''; if ( $this->item->values['min-bedrooms'] == '4' ) { $minbedrooms4check = 'selected="selected"'; } $minbedrooms5check = ''; if ( $this->item->values['min-bedrooms'] == '5' ) { $minbedrooms5check = 'selected="selected"'; } ?> <select id="min-bedrooms" name="min-bedrooms" class="profile-select"> <option value="" <?= $minbedroomscheck ?>>min bedrooms</option> <option value="0" <?= $minbedroomsanycheck ?>>any</option> <option value="1" <?= $minbedrooms1check ?>>1+</option> <option value="2" <?= $minbedrooms2check ?>>2+</option> <option value="3" <?= $minbedrooms3check ?>>3+</option> <option value="4" <?= $minbedrooms4check ?>>4+</option> <option value="5" <?= $minbedrooms5check ?>>5+</option> </select>
you can below,
- first store min-bedrooms count in database
- then store min-bedrooms selected value in database,
the retrieve min-bedrooms count , min-bedrooms selected value , loop below
<?php $min_bedrooms = 6; // should stored in database $min_bedrooms_selected = 4; //$this->item->values['min-bedrooms'] ?> <select class="profile-select" name="min-bedrooms" id="min-bedrooms"> <?php for($i = 0; $i < $min_bedrooms; $i++){ ?> <?php if($i == $min_bedrooms_selected){ ?> <option value="<?php echo $i;?>" selected="selected"> <?php echo $i;?></option> <?php }else{ ?> <option value="<?php echo $i;?>"> <?php echo $i;?></option> <?php } ?> <?php } ?> </select>
Comments
Post a Comment