Checkbox error

0

I need to make a while in PHP :

  while($lo = mysql_fetch_array($ro)) {
      $id3 = $lo["id"];
      $nome_menor = utf8_encode($lo["nome_menor"]);

That's okay! Showing the companies listed in the query below:

$ro = mysql_query("select * from empresa order by nome_menor");

But now I need to make a if within while

I need the if check in the checkbox table if the variable name of while is 1 or 0, I thought of something like this:

$rz = mysql_query("select * from mulempresa where (id_contrato = $id2) ");
$ck1 = mysql_fetch_array($rz,MYSQLI_ASSOC);

  $checked = "";
  if($ck1[$nome_menor] == 1)
  $checked3 = "checked=checked";

But you are giving the following error:

"Notice: Undefined index"

How can I resolve this?

    
asked by anonymous 16.06.2016 / 16:25

3 answers

2

Try this way >

$rz = mysql_query("select * from mulempresa where id_contrato = $id2 ");
    $ck1 = mysql_fetch_array($rz);
    while ($lo = mysql_fetch_array($ro)) {
        $id3 = $lo["id"];
        $nome_menor = utf8_encode($lo["nome_menor"]);
        $checked = "";
        if ($ck1[$nome_menor] == 1) {
            $checked = "checked='checked'";
        }
        echo "<input type='checkbox' name='{$id3}' onclick='return false' value='on' {$checked} />{$nome_menor}<br/>";
    }

NOTE: There is a big problem with this if > if ('$ck1[$nome_menor]' == 1) . single quotation marks will not print the value of your variable but rather the name. Double quotes do the opposite, that is, printa the value of the variable.

    
16.06.2016 / 17:15
1

Leave it like this:

 $checked3 = "checked";

 echo "<input type='checkbox' name='$id3' onclick='return false' value='on' ". $checked3 ." /> ".$nome_menor." <br/> ";

Must resolve.

    
16.06.2016 / 16:58
1

I solved it! I changed the variable by a counter! Based on the position of the array, then I collect the data inside! Thanks to everyone's help! Here is the code:

    <?php
$i=1;
$rz = mysql_query("select * from mulempresa where id_contrato = $id2 ");
    $ck1 = mysql_fetch_array($rz);
    print_r($ck1);
    while ($lo = mysql_fetch_array($ro)) {
        $id3 = $lo["id"];
        $nome_menor = utf8_encode($lo["nome_menor"]);
        $checked = "";

        if ($ck1[$i] == 1) {
            $checked = "checked='checked'";
        }
        echo "<input type='checkbox' name='{$id3}' onclick='return false' value='on' {$checked} />{$nome_menor}<br/>";
$i = $i + 1;
}?>
    
16.06.2016 / 19:21