divide chain separated by a comma and validate if it is the same

2

this is the code I have

variable that I bring from the database $ profile ['image']; < --- in this variable I can bring several options for example:

presentable,aseado
presentable,aseado,De traje
Presentable,aseado,uniforme

so if for example the variable brings presentable, neat in the option I would like to do a if($perfil['imagen'] == 'presentable'){ //codigo}

Something like this if you could give me an example

$perfil['imagen'];
                    <option if($perfil['imagen'] == 'presentable'){ echo "selected";} value="presentable">Presentable</option>
                    <option if($perfil['imagen'] == 'aseado'){ echo "selected";} value="aseado">Aseado</option>
                    <option if($perfil['imagen'] == 'traje'){ echo "selected";} value="traje">Traje</option>
                    <option if($perfil['imagen'] == 'condicion saludable'){ echo "selected";} value="condicion saludable">Condicion saludable</option>
                    <option if($perfil['imagen'] == 'sin tatuajes'){ } value="sin tatuajes">Sin tatuajes</option>
                    <option if($perfil['imagen'] == 'sin percing'){ } value="sin percing">Sin percing</option>
                    <option if($perfil['imagen'] == 'sin expansiones'){ } value="sin expansiones">Sin expansiones</option>

I would like something similar to this to validate if the value of the varible $ profile ['experience'] that comes from the database with some of the values of the option is equal

is an example I made but the variable $ profile ['experience'] (6 months) only brought a value

$perfil['experiencia'] == "6 meses" 
                      <option <?php if($perfil['experiencia'] == "6 meses"){ echo "selected"; } ?> value="6 meses">6 meses</option>
                      <option <?php if($perfil['experiencia'] == "1 año"){ echo "selected"; } ?> value="1 año">1 año</option>
                      <option <?php if($perfil['experiencia'] == "2 años"){ echo "selected"; } ?> value="2 años">2 años</option>
                      <option <?php if($perfil['experiencia'] == "5 años"){ echo "selected"; } ?> value="5 años">3 años</option>

Here is the solution that I needed to validate the options the correct question was to look inside an array if the word x existed      value="presentable"> Presentable

    
asked by Carlos Enrique Gil Gil 02.02.2018 в 19:26
source

1 answer

5

You could use the explode function, that is, divide a string into several strings.

Possible example:

//Tu variable $perfil['experiencia']
$data = 'presentable,aseado,uniforme';

//Divide una cadena.
$cadena = explode(",", $data);      

//Recorrer array
foreach ($cadena as $valor) {       
    if ($valor == 'presentable') {
        //Sigues con tu codigo
    } 
}

Your second question, I'm not entirely clear what function you really want to create. Let's see if I can still give you a little guidance with him foreach .

//Recorrer array y creamos inputs segun 'valor'.
foreach ($cadena as $valor) {       
    if ($valor == 'presentable') {
        echo "<option value=$valor selected>$valor</option>";               
    } elseif ($valor == 'aseado') {
        echo "<option value=$valor selected>$valor</option>";
    } else {
        //Default valores.
    }
}


  

At the end @CarlosEnriqueGilGil has solved your second question in the following way.

Add the selected attribute to your option if it exists in our array ( $cadena ).

<?php

//Tu variable $perfil['imagen']
$data = 'presentable,aseado,uniforme';

//Dividir una cadena.
$cadena = explode(",", $data);      

#Añadir el atributo 'selected' en caso si existe en nuestro arreglo ($cadena) mediante la función
#in_Array(), es decir, comprueba si un valor existe en un array.
echo"<option "; if(in_array("presentable",$cadena)) echo 'selected';  echo " value='presentable'>Presentable</option>\n";
echo"<option "; if(in_array("aseado",$cadena)) echo 'selected';  echo " value='aseado'>Aseado</option>\n";
echo"<option "; if(in_array("de traje",$cadena)) echo 'selected';  echo " value='de_traje'>De traje</option>\n";

Result:

<option selected value='presentable'>Presentable</option>
<option selected value='aseado'>Aseado</option>
<option  value='de_traje'>De traje</option>

DEMO

    
answered by 02.02.2018 / 19:51
source