Error in the Checkbox

Asked

Viewed 119 times

0

I need to make a while in the PHP:

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

This is ok! This shows the name of the companies listed on query down below:

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

But now I need to make one if within the while

I need the if check in the checkbox table if the variable name of the while 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 the following mistake is making:

"Notice: Undefined index"

How can I fix this?

  • better you post the entire code. Because the error might be somewhere else. Generally, this error is caused because you have not rescued a variable. That’s why she’s undefined.

3 answers

2


Try it that 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/>";
    }

OBS: There is a big problem in this if > if ('$ck1[$nome_menor]' == 1). single quotes will not print the value of your variable but the name. Double quotes does the opposite, ie prints the value of the variable.

  • Unfortunately it was not, it gives this error: Notice: Undefined index ( Name of the column of the table in the query) !

  • the Table looks like this: Exactly the name $name_minor as column name and 0 or 1 as value within each company

  • of a print_r in $Rz and paste here if possible

  • I solved it! I changed the name_minor to a counter! thanks!

  • Good, I was checking your $Rz array and had not found the Indice 'minor name_name'

1

I solved it! I switched the variable for a counter! Based on the array position, then I collect the dice inside! Thanks to Help from all! Follows 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;
}?>

1

Leave it at that:

 $checked3 = "checked";

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

Must solve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.