Check with php and disable option

Asked

Viewed 67 times

0

I know what you have to do, but I don’t know how to do it.

I have the following array:

 $dados =  array
                       0 =>
                           array
                              'id' => 1
                              'posicao' => 1
                              'ativo' => 'sim'
                       1 =>
                           array
                              'id' => 2
                              'posicao' => 2
                              'ativo' => 'sim'
                       2 =>
                           array
                              'id' => 3
                              'posicao' => 3
                              'ativo' => 'sim'
                       3 =>
                           array
                              'id' => 4
                              'posicao' => 4
                              'ativo' => 'nao'
                       4 =>
                           array
                              'id' => 5
                              'posicao' => 5
                              'ativo' => 'nao'

I list each of these items with a loop for and in each one I have this select:

            <select>
                <?php for ($i=0; $i < count($dados); $i++) {  //para cada item do array eu faço um option ?>
                    <h3>Posição</h3>
                    <option value="<?php echo $dados[$i]['posicao'] ?>" <?php echo $selected = ($dados[$i]['posicao'] == $pos)? 'selected' : ' '; //verifico qual a posicao do item e seleciono?> ><?php echo $dados[$i]['posicao'] ?></option>
                <?php   } ?>
            </select>

So far it is correct, now I need to check in each item of the array if it is active and if it is disabled the option.

Follow image for didactic question:

Itens

2 answers

0

You can make it look like what was done to display the selected, follows the code below:

<select>
    <?php for ($i=0; $i < count($dados); $i++) {?>
     <h3>Posição</h3>
         <option value="<?php echo $dados[$i]['posicao'] ?>" <?php echo $selected = ($dados[$i]['posicao'] == $pos)? 'selected' : ' ';?> <?=($dados[$i]['ativo'] == 'nao' ? 'disabled' : '')?>><?php echo $dados[$i]['posicao'] ?></option>
     <?php   } ?>
</select>
  • I even thought about making this format, but as I commented this code will be inside another loop as well. That is, if position 1 has an item ativo it will need to appear for the other item as well and so on.

  • I think it will work normal

0


I managed to make the following format

        <select name="posicao" id="posicao">
            <option  value="null" <?php echo $selected = ($dados[$i]['posicao'] == 'null')? 'selected' : ' '; ?>>Selecione</option>
            <?php 
            for ($i = 0; $pos < $i; $i++) { ?> 
                <option value="<?php echo $i ?>" <?php echo $selected = ($dados[$i]['posicao'] == $i)? 'selected' : ' '; ?>
                  <?php

                  //criei um loop com um if 

                    for ($ativo=0; $ativo < count($dados); $ativo++) { 
                        if ($i == $dados[$ativo]['posicao'] && $dados[$ativo]['ativo'] == 'sim') {
                            echo 'disabled';
                        }
                    }
                  ?>
                  ><?php echo $i; ?></option>
            <?php } ?>
        </select>

Browser other questions tagged

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