Bring checkeado from dynamic radio button database?

1

I have this radio button that fills up from the database dianamically, In what way could your database be checked from the database for editing? , I would be served either in jquery or php greetings thank you!

<div class="inline-group">   
<?php foreach ($Ubicacion as $key => $value) { ?>
<label class="radio">    
    <input id="<?= $value->nombre?>" type="radio" class="radio" value="<?= $value->id_ubicacion?>" name="ubicacion"/><?= $value->nombre?>
    <i>
    </i>
    </input>
</label>
<?php } ?>
 </div>

This data is a foreign key, when calling the array $ Location I fill the foreach with the data of that array, in another array I have the data that is stored, of what form within that foreach I can compare the id of the radius button with the id of the array in my database to bring the data checked?

    
asked by Javier Antonio Aguayo Aguilar 28.03.2017 в 04:03
source

3 answers

1

if from the database you have the field "this_checked", compare if it is true and print it in the middle of the input (for a radio to be checked it must have the checked attribute). I hope you serve, greetings.

<?= $value->esta_checkeado == true ? "checked" : "" ?>

it would be like this:

<div class="inline-group">   
<?php foreach ($Ubicacion as $key => $value) { ?>
<label class="radio">    
    <input id="<?= $value->nombre?>" <?= $value->esta_checkeado == true ? "checked":""=> type="radio" class="radio" value="<?= $value->id_ubicacion?>" name="ubicacion"/><?= $value->nombre?>
    <i>
    </i>
    </input>
</label>
<?php } ?>
 </div>
    
answered by 05.05.2017 в 18:26
0

I suggest the following:

// Pasar los IDs seleccionados a un array
$IdSeleccionados=array();

foreach ($Seleccionados as $key => $value) {
   $IdSeleccionados[]=$value->ID; // "ID" es el dato a comparar con $value->id_ubicacion
}

Then:

<div class="inline-group">   
<?php foreach ($Ubicacion as $key => $value) { ?>
<label class="radio">    
    <input id="<?= $value->nombre?>" type="radio" class="radio" value="<?= $value->id_ubicacion?>" name="ubicacion" <?= in_array($value->id_ubicacion, $IdSeleccionados)? ' checked="checked" ':'' ?>  /><?= $value->nombre?>
    <i>
    </i>
    </input>
</label>
<?php } ?>
 </div>
    
answered by 28.03.2017 в 09:16
0

Use the checked property <?php ($value->id_ubicacion === $ubicacion->id_ubicacion) ? 'checked' : ''?>/><?= $value->nombre?> /> :

<div class="inline-group">   
<?php foreach ($Ubicacion as $key => $value) : ?>
<label class="radio">    
    <input id="<?= $value->nombre?>"
           type="radio"
           class="radio"
           value="<?= $value->id_ubicacion?>"
           name="ubicacion" 
           <?php echo ($value->id_ubicacion === $ubicacion->id_ubicacion) ? 'checked' : ''?>/><?= $value->nombre?> />
    <i>
    </i>
    </input>
</label>
<?php endforeach; ?>
 </div>
    
answered by 20.10.2017 в 20:54