Question: I have a select dropdown with three options in it; based on the referring URL, I need each option to be the one selected (ie selected="selected"). 

I'm pretty new to PHP but can do it this way:

```
       <?php if (strstr( $_SERVER['HTTP_REFERER'],"url-1" )) {
            ?>
               <option value="value-1" selected="selected">
            <?
         } else {
            ?>
            <option value="value-1">
            <?
         }                           
      ?>

```

I do this for each of the three options in the dropdown. As said, this works -- but it seems pretty inefficient to repeat the same thing for each option. Anyone got any clues for me? Thanks in advance for your time.

EDIT: 

Thanks everyone for all for the help, particularly Adnan. Figured this my ideal solution with help from another forum search, as well as your thoughts: 

```
<?php 
$options = array(array('value' => 'value-one', 'label' => 'value-one', 'ref' => 'url-one'),array('value' => 'value-two', 'label' => 'value-two', 'ref' => 'url-two'),array('value' => 'value-three', 'label' => 'value-three', 'ref' => 'url-three'),
);  
foreach ($options as $option) {
if (strpos($_SERVER['HTTP_REFERER'], $option['ref']) > 0) {
$optSelected = ' selected';
} else {
$optSelected = ''; 
}
echo '<option value="' . $option['value'] . '"' . $optSelected . '>' . $option['label'] .     '</option>';}
?>` 

```

Answer: 
Here's a "cool" way of doing it

```
foreach(array('url-1', 'url-2', 'url-3') as $url){
    echo (strstr ($_SERVER['HTTP_REFERER'], $url))?'<option value="value-'.substr($url, 4, 1).'" selected>':'<option value="value-'.substr($url, 4, 1).'">';
}

```

note: `selected` attrib is boolean, so you don't need to give it a value, just put it there.

Update: Here's a correction for the code in your comment:

```
foreach (array('url1' => 'value-one', 'url2' => 'value-two', 'url' => 'value-three') as $url => $value){
   echo (strstr($_SERVER['HTTP_REFERER'], $url))?"<option value=\"$value\" selected>$value</option>":"<option value=\"$value\">$value</option>";
}

```
You could always use switch(case) statement instead? itll stop you from having to call 

```
strstr( $_SERVER['HTTP_REFERER'],"url-1" )

```

multiple times?