为什么如果在html选择taq的选项taq中两者都是真的

Its realy strange..what is wrong with this one..?? I want to selected one option in my select drop down by default.so if a value is poseted my if condition got true then why the else is also executing.Your Help Will be really appreciated.

<select name="location" id="location" class="selector_holder_select">

<?php foreach($location as $loc){ ?>

<option value="<?php echo $loc['deal_location_id']; ?>"

<?php if($loc['deal_location_id']==$_POST['location']){ echo 'selected="selected"'; }

else { 

if(strcasecmp($loc['location'], $city['cityName']) == 0) { echo 
'selected="selected"';     }

 } ?>><?php echo $loc['location']; ?></option>

 <?php } //End of Foreach ?> 

 </select>
  Above not solve my issue.However this one worked.But can any body make it          
  shorter  and cleanup so it looks more professional.


   <select name="location" id="location" class="selector_holder_select">
   <?php foreach($location as   $loc){  ?>                  
    <option value="<?php echo $loc['deal_location_id']; ?>"
    <?php               
   if($loc['deal_location_id']==$_POST['location'])
   {
    echo 'selected="selected"'; 
   } 
   else {     
        if(strcasecmp($loc['location'], $city['cityName']) == 0 and !isset($_POST['location']))
        { echo 'selected="selected"'; } 
   } ?>>
<?php echo $loc['location']; ?></option>

 <?php } ?>
 </select>

One problem with this code is that you mix HTML and PHP quite heavily. This makes is very hard to read. I recommend using HEREDOC or something similar to create all the HTML parts instead of switching PHP on and off that often.

I guess what happens is that once in the foreach loop the first if-condition is met and once (in a different iteration step) the elseif is met. That way two elements get selected.

The problem is, that you cannot solve this problem in a single foreach. You first have to check whether the if-case will be true at some point. If so you can set the respective field selected. If not you can search for the else-if and handle that.

try this:

<select name="location" id="location" class="selector_holder_select">
<?php foreach($location as $loc){
    echo('<option value="'.$loc['deal_location_id'].'"')
    if($loc['deal_location_id'] == $_POST['location'] || strcasecmp($loc['location'], $city['cityName']) == 0)
        echo(' selected="selected"');
    echo(">".$loc['location']."</option>");
?>
</select>

However I recommend you to follow a tutorial about the basics of PHP, for example this from W3C. It looks like you've got no PHP-experience whatsoever.

Try this:

<select name="location" id="location" class="selector_holder_select">
    <?php 
    foreach($location as $loc) {
            echo "<option value=$loc['deal_location_id']>";

            if($loc['deal_location_id']==$_POST['location']) { 
                echo 'selected="selected"'; 
            } else { 
                if(strcasecmp($loc['location'], $city['cityName']) == 0) { 
                    echo 'selected="selected"';     
                }
            }

            echo "$loc['location']</option>";
  }
</select>