Show BD value in Dropdown Box

Asked

Viewed 98 times

1

I have in my database a value for user level and I would like to be able to modify it in the change. I need to load the level of the logged in user when opening the page:

<div class="form-group">
    <label>Nivel de Usuário</label>
    <select class="form-control" name="">
        <option value="2">Usuário Comum</option>
        <option value="1">Administrador</option>
    </select>
</div>

In my Session I already have the user ID and have already done the select, I just need to take this level (of the selected user), for example "1" and, when opening the page, show the option <option value="1">Administrador</option>.

The question of user validation for each area is quiet to do. My doubt at the moment is only this in relation to the Dropdown HTML with PHP.

From now on I thank everyone and all the answers!

  • you want to apply the selected option? these values are fixed or come from the bank?

3 answers

1

You can check in the looping you do to add the options if the ID is equal to ID of the logged-in user level

$nivelUsuarioLogado = 2;

$niveis = array(
    1=>'Administrador',
    2=>'Usuário Comum'
);

echo '<div class="form-group">';
    echo '<label>Nivel de Usuário</label>';
    echo '<select class="form-control" name="">';
        foreach($niveis as $key => $value){
            $selected = ($nivelUsuarioLogado == $key) ? true : false;

            echo '<option value="'.$key.'" '.(($selected) ? 'selected="selected"' : '').'>';
                echo $value;
            echo '</option>';
        }
    echo '</select>';
echo '</div>';

1

Can simplify the check with the value that should have Selected with printf.

<select name="nivel">
   <?php
       foreach($niveis as $item){
          $select = $item['id'] == $nivel_session ? $select = 'selected = "selected"' : "";
          printf('<option value="%d">%s</option>'. $item['id'], $item['nivel_descricao']);
       }
   ?>
</select>
  • I don’t understand exactly. where does this 'level description' come from? i do not have in the database the names, only numbers 1 and 2 that indicate these levels @rray

  • @Lucas if it is a database array

  • I managed to solve here from a simple if. I had not thought about it. I stayed a long time breaking my head until I could solve. I will publish the solution below. Anyway, thank you very much for the answer, she gave me a light to be able to solve!

1


<div class="form-group">
    <label>Nivel de Usuário</label>
    <select class="form-control" disabled="true">
        <option value="2" <?php if ($$row_listaMe['nivel'] == 2){echo "selected";}; ?>>Usuário Comum</option>
        <option value="1" <?php if ($row_listaMe['nivel'] == 1){echo "selected";}; ?>>Administrador</option>
    </select>
</div>
  • You had not said whether the options were fixed or not, if you managed to solve it by :). If the options increase a lot think about saving in the bank, because that way you will need an if for each option, imagine now with 10 options or more.

  • @True, this is true what I’m going to pass further in the construction of this site. I might need a select with approximately 20/25 options. In that case I’ll see how I do and maybe I’ll open another topic xD Thank you very much rray, again!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.