Take sql data and mark it as active or current

3

I want to do this in php maybe I can not explain myself well so if you need to modify it do not worry I can do it quickly since I feel in my pc

I have a table with only 2 variables (ACTIVE) and (NOT ACTIVE)

$sql = mssql("select estado from table where item = 'pesado'"); 
$temp = mssql_fetch_array ($sql);

I need the type = 'radio' checked if it's active How can I do it from a select?

<td WIDTH = 100> 
    <input size='1' type ='radio' value='ACTIVO' name='estado'>SI 
    <input size='7' type ='radio' value='NO ACTIVO' name='estado'>NO
</td>

OK friends as you will see right away I saw 4 comments activate me to try and try this but it gives me error

will be more concrete:

<?php
include "../../includes/db_connect_mysql.php"; 
$sql = mysqli_query($conexion,"
SELECT 
id, 
nombre, 
nombre_esp, 
nombre_eng, 
nombre_bra, 
orden, 
estado, 
id_padre, 
seccion
FROM sr_productos_categorias
WHERE orden > 0 and seccion = 'tienda' ORDER BY orden ASC
");
if (mysqli_num_rows($sql) > 0){

echo
"Item a Editar;
<table border=1 aling=left WIDTH= 580>
<tr>

 <td WIDTH= 280><font color=#FF000> Categoria Nombre: </font></td>
 <td WIDTH= 50><font color=#FF000> Orden: </font></td>
 <td WIDTH= 100><font color=#FF000> Estado: </font></td>
 <td WIDTH= 110><font color=#FF000> Sub-Categoria: </font></td>

</tr>
</table>
"; while ($temp[6] = mysqli_fetch_array($sql))
{
print
"<table border=1 aling=left WIDTH= 580>
<tr>

 <td WIDTH = 280><input type ='radio' name='codigo' value='".$temp["codigo"]."'>".$temp["nombre_esp"]."</td>
 <td WIDTH = 50><input size='1' type='text' placeholder = 'orden' name='orden' value='".$temp["orden"]."' /></td>
    <td WIDTH = 100> 
        <input size='1' type ='radio' 
";
<?php ($temp[6] === true ? echo 'checked'  : null) ?> 
print
"
value='ACTIVO' name='estado'>SI 
        <input 
";
<?php ($temp === false ? echo 'checked'  : null) ?>
print
"
 size='7'  type ='radio' value='NO ACTIVO' name='estado'>NO
    </td>
 <td WIDTH = 110><name='orden' /><a href='#'>Editar</a></td>

</tr>
</table>
";}
 echo
"
"
 ;} else{
 echo"No hay datos";}
?>

What do I have to correct?

    
asked by Juan Carlos Villamizar Alvarez 26.01.2017 в 12:52
source

3 answers

3

When painting those checkboxes you should check the result of the query and paint checked if it is active.

<td WIDTH = 100> 
  <input size='1' type ='radio' id='ACTIVO' <?php $estado == 'activo' ? echo 'checked' : null; ?> name='estado'>SI 
  <input size='7' type ='radio' id='NO ACTIVO' <?php $estado == 'no activo' ? echo 'checked' : null; ?> name='estado'>NO
</td>
    
answered by 26.01.2017 в 13:01
3

You should evaluate the result of the sql statement, which will return the state, I understand that it will be a boolean value. You have to store that value in a variable, for example: $ status To mark it as selected you have to make a conditional, in this case the ternary go very well.

    <td WIDTH = 100> 
        <input size='1' type ='radio' <?php ($status === true ? echo "checked"  : null) ?> value='ACTIVO' name='estado'>SI 
        <input <?php ($status === false ? echo "checked"  : null) ?> size='7'  type ='radio' value='NO ACTIVO' name='estado'>NO
    </td>

I hope the idea can help you solve your problem.

    
answered by 26.01.2017 в 13:02
-1

You can use JS:

var resultado = "<?php echo $sql; ?>";

I changed your attribute value by ID
Assuming the result is like the ID ("ACTIVE", "NOT ACTIVE") you can do:

<td WIDTH = 100> 
    <input size='1' type ='radio' id='ACTIVO' name='estado'>SI 
    <input size='7' type ='radio' id='NO ACTIVO' name='estado'>NO
</td>


document.getElementById(resultado).checked = true;
    
answered by 26.01.2017 в 12:59