Use function to compare value and mark radio buttom

Asked

Viewed 50 times

-4

I need to create a function to compare value and mark radio buttom.

$checklistIni = unserialize($row_rsRegistros['checklistIni']);
$a = join(',', array_values($checklistIni));

function compara($valor) {
    $array = explode(",", $a);
    if(in_array($valor, $checklistIni)) {
        return "checked";
    }
}

And then use right where I need to make the comparison.

<tbody>
   <tr>
        <td class="texto-branco">Documento</td>
        <td><label><input value="tem" <?php echo compara("tem"); ?>  name="checklistFim0" class="checklistFim" type="radio" /><span></span></label></td>
        <td><label><input value="nao" <?php echo compara("nao"); ?>  name="checklistFim0" class="checklistFim" type="radio" /><span></span></label></td>
        <td><label><input value="ava" <?php echo compara("ava"); ?>  name="checklistFim0" class="checklistFim" type="radio" /><span></span></label></td>
    </tr>
        <tr><td class="texto-branco"> Manual </td>
        <td><label><input value="tem" <?php echo compara("tem"); ?>  name="checklistFim1" class="checklistFim" type="radio" /><span></span></label></td>
        <td><label><input value="nao" <?php echo compara("nao"); ?>  name="checklistFim1" class="checklistFim" type="radio" /><span></span></label></td>
        <td><label><input value="ava" <?php echo compara("ava"); ?>  name="checklistFim1" class="checklistFim" type="radio" /><span></span></label></td>
    </tr>
        <tr><td class="texto-branco">Vidro eletrico </td>
        <td><label><input value="tem" <?php echo compara("tem"); ?>  name="checklistFim2" class="checklistFim" type="radio" /><span></span></label></td>
        <td><label><input value="nao" <?php echo compara("nao"); ?>  name="checklistFim2" class="checklistFim" type="radio" /><span></span></label></td>
        <td><label><input value="ava" <?php echo compara("ava"); ?>  name="checklistFim2" class="checklistFim" type="radio" /><span></span></label></td>
    </tr>
        <tr><td class="texto-branco">Radio/CD </td>
        <td><label><input value="tem" <?php echo compara("tem"); ?>  name="checklistFim3" class="checklistFim" type="radio" /><span></span></label></td>
        <td><label><input value="nao" <?php echo compara("nao"); ?>  name="checklistFim3" class="checklistFim" type="radio" /><span></span></label></td>
        <td><label><input value="ava" <?php echo compara("ava"); ?>  name="checklistFim3" class="checklistFim" type="radio" /><span></span></label></td>
    </tr>
</tbody>
  • something like echo $key1 . ","?

  • 8

    Clear example of XY problem. There is no justification for you from a array, generate a string, to then pass the value (as global variable?) to a function that will convert back to array. The solution seems very ineffective and you can solve your problem by preventing it from happening, making it more plausible.

1 answer

0


You need to pass the index of the list of values you want to test in order to compare. See:

$checklistIni = unserialize($row_rsRegistros['checklistIni']);

function compara($array, $index, $valor) {
    if(isset($array[$index] && $array[$index] == $valor)) {
        return "checked";
    }
    return '';
}

// printar o atributo checked SOMENTE se o primeiro item da listagem for "tem"
<input value="tem" <?php echo compara($checklistIni, 0, "tem"); ?> />

The above example assumes that the function result unserialize is an array with numeric indices. If the result is an associative array, simply swap the variable declaration $checklistIni for:

$checklistIni = array_values(unserialize($row_rsRegistros['checklistIni']));
  • Hello Vinicius, I am using the third option, but it is not marking. Or better, this, but all of the same column corresponding to Ava.

  • I edited my answer because I had misunderstood your question, because the title of your question does not match the context of what you want to do.

  • This is my canvas http://prntscr.com/o1yecx

  • Error 500 on page

  • 1

    Your question is a bit confused and with a title that does not reflect your real problem and still with incomplete content. I suggest you take a look at How to create a Minimum, Complete and Verifiable example and after that edit your question including the section where you print the checkbox field (and not just the html of the direct field as it is in the question) so that we can help you.

  • Not one last try? This is my canvas https://prnt.sc/o1yecx

  • I did the Vinicius edition. See if I could explain better, I’m sorry, I’m terrible with words and expression.

  • Could include what is being stored on $checklistIni ?

Show 3 more comments

Browser other questions tagged

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