change the value of an option within an array

1

It happens that I have a array with the following data:

$sistemas2 = array('BILLETERO', 'VIDEO', 'CONTROL DE ACCESO', 
'CONTROL DE ACCESO  VIDEO', 'ALARMA', 'ALARMA   VIDEO');

And I go through it in a select to generate a <option> for each data that is found in the following way:

foreach ($sistemas2 AS $valor2) { ?>
<option <?php if ($sistemas2 == $valor2){ 
?>selected <?php } 
?>> <?php  echo $valor2; ?>
</option> <?php                                    
}
}
?>

What I want is that each option has a specific value different from the one shown by the array, to make a query to the database. example:

<option value="controles">BILLETERO</option>
    
asked by Edgar Yezid Cruz 20.04.2018 в 18:13
source

2 answers

0

This is how I use it, where $programa->get('id_facultad') is the id of the current faculty and $row['id_facultad'] is the arrangement of faculties

$activo = ($programa->get('id_facultad') == $row['id_facultad']) ? "selected" : "";
echo "<option ".$activo ." value='".$row['id_facultad']."'>".$row['nombre_facultad']."</option>";
    
answered by 20.04.2018 / 18:48
source
0

You could do it with an associative array like this:

$sistemas2 = array('controles'=>'BILLETERO', 'v'=>'VIDEO','c'=> 'CONTROL DE ACCESO', 'cv'=>'CONTROL DE ACCESO  VIDEO','a'=> 'ALARMA', 'av'=>'ALARMA   VIDEO');

and then you go through it like this:

foreach ($sistemas2 AS $key => $valor2) { ?>
    <option value="<?php echo $key.'"'; if ($sistemas2 == $valor2){ ?>selected <?php } ?>> <?php  echo 
    $valor2; ?></option> <?php                                    
    }
 }?>
    
answered by 20.04.2018 в 18:21