Change option from select to URL

1

I have a select with five option . I want it to check the link that the user is and according to that link, he selects an option.

My HTML:

<select name="segmento" id="segmento" class="basic">
  <option value="">Segmento</option>
  <option value="Teste Engenharia">Teste Engenharia</option>
  <option value="Teste Hospitalar">Teste Hospitalar</option>
  <option value="Teste Ilumina&ccedil;&atilde;o">Teste Ilumina&ccedil;&atilde;o</option>
  <option value="Teste Elevadores">Teste Elevadores</option>
</select>

I'm trying to do via PHP with preg_match , but my knowledge is limited, so I'm probably doing something wrong.

What I'm trying to do:

<option
<?php 
if (preg_match( '/segmentos\/teste-hospitalar/',$database->parametros['menuRoteador']))
    echo 'value="teste Hospitalar" Teste Hospitalar';
elseif (preg_match('/segmentos\/neomot-elevadores/',$database->parametros['menuRoteador']))
    echo 'value="Teste Elevadores" Teste Elevadores';
elseif (preg_match('/segmentos\/neomot-engenharia/',$database->parametros['menuRoteador']))
    echo 'value="Teste Engenharia" Teste Engenharia';
?>
>
</option>

It should be occupied by the value of <option value="">Segmento</option> , but this is not happening.

    
asked by anonymous 05.02.2015 / 13:53

1 answer

3

You can improve your logic, but I believe your error is only in the% s of% s, since you are forgetting the echo character of the > tag.

For example this option :

value="teste Hospitalar" Teste Hospitalar

It should look like this:

value="teste Hospitalar">Teste Hospitalar

An alternative to simplifying code visualization looks like this:

<?php 
if (preg_match( '/segmentos\/teste-hospitalar/',$database->parametros['menuRoteador']))
    $valor = 'teste Hospitalar';
elseif (preg_match('/segmentos\/neomot-elevadores/',$database->parametros['menuRoteador']))
    $valor = 'Teste Elevadores';
elseif (preg_match('/segmentos\/neomot-engenharia/',$database->parametros['menuRoteador']))
    $valor = 'Teste Engenharia';
?>
<option value="<?= $valor ?>"><?= $valor ?></option>
    
05.02.2015 / 14:11