Foreach with database records

Asked

Viewed 1,669 times

2

In my database I have a table called cadastroatendo within this table I put the days of attendance. I am recovering the data through the foreach... which in turn the array returns this result:

    [listaAtendo] => Array
    (
        [0] => stdClass Object
            (
                [codCadastroAtendo] => 46
                [codCadastroPerfil] => 50
                [codCadastroAtendoCat] => 7
            )

        [1] => stdClass Object
            (
                [codCadastroAtendo] => 47
                [codCadastroPerfil] => 50
                [codCadastroAtendoCat] => 8
            )

        [2] => stdClass Object
            (
                [codCadastroAtendo] => 48
                [codCadastroPerfil] => 50
                [codCadastroAtendoCat] => 9
            )

    )

I display the fields to be as "checked" case the codCadastroAtendoCat is equal to the id shown in the listing. I list this way:

Model:

$sql_atendo = "SELECT * FROM cadastroatendo WHERE codCadastroPerfil = {$id}";
$valor->listaAtendo = $this->db->query($sql_atendo)->result();

View:

<div class="line" style="height: 70px;">
<table style="width: 100%;">
<? 
            $contagem = 0;
            foreach($this->data['parametroAtendo'] as $valor){ 
                if($contagem==0){ 
            ?>
    <tr>
    <? } $contagem++; ?>
        <td style="300px;">
            <input type="checkbox" name="atendo_codCadastroAtendoCat[]" value="<? echo $valor->idParametro; ?>" id="1_<? echo $valor->idParametro; ?>">&nbsp;&nbsp;<label for="1_<? echo $valor->idParametro; ?>" style="text-align: left;"><? echo $valor->parametro; ?></label>
        </td>
    <? if($contagem==4){ $contagem = 0; ?>
    </tr>
<? } } ?>
</table>
</div>

I need: when I am registered in the database table that ID, with that Profile, the field is as "checked". Someone can help me?

Explanation:

I have checkbox fields, which are previously registered in the database. I list all of them on the screen, and I write the selected ones in the table... I do the recovery, which comes through an array(), however, I cannot check if there is this record in the database, to put it as checked, when editing.

2 answers

2


I think this should take care.

Basically, with each iteration of the array atendo you have to check if the Id of this element exists within the array of your call list, and by that say whether it will have the property checked or not.

<div class="line" style="height: 70px;">
<table style="width: 100%;">
<?php
            $contagem = 0;
            foreach($this->data['parametroAtendo'] as $valor){ 
                $checkado = '';
                foreach ($listaAtendo as $obj) {
                    $codCadAtendendoCat = (int)$obj->codCadastroAtendoCat;
                    if ($valor->idParametro == $codCadAtendendoCat) {
                        $checkado = ' ckecked="checked"' : '';
                        break;
                    }
                }
                if($contagem==0){ 
            ?>
    <tr>
    <? } $contagem++; ?>
        <td style="300px;">
            <input type="checkbox" name="atendo_codCadastroAtendoCat[]" value="<? echo $valor->idParametro; ?>"<?php echo $checkado?> id="1_<? echo $valor->idParametro; ?>">&nbsp;&nbsp;<label for="1_<? echo $valor->idParametro; ?>" style="text-align: left;"><? echo $valor->parametro; ?></label>
        </td>
    <? if($contagem==4){ $contagem = 0; ?>
    </tr>
<? } } ?>
</table>
</div>

0

Your reply was valid. I wrote otherwise also, that worked...

            <div class="line" style="height: 70px;">
                <table style="width: 100%;">
                <? 
                $contagem = 0;
                foreach($this->data['parametroAtendo'] as $valor){
                    if(empty($this->uri->segment(3))){ $lista = 0; } else { $lista = $this->acompanhantes_model->getByIdCategory($this->uri->segment(3), $valor->idParametro, 'cadastroatendo', 'codCadastroAtendoCat'); }
                    if($contagem==0){ 

                ?>
                    <tr>
                    <? } $contagem++; ?>
                        <td style="300px;">
                            <input type="checkbox" name="atendo_codCadastroAtendoCat[]" value="<? echo $valor->idParametro; ?>" <? if($lista>0){ echo "checked"; } ?> id="1_<? echo $valor->idParametro; ?>">&nbsp;&nbsp;<label for="1_<? echo $valor->idParametro; ?>" style="text-align: left;"><? echo $valor->parametro; ?></label>
                        </td>
                    <? if($contagem==4){ $contagem = 0; ?>
                    </tr>
                <? } } ?>
                </table>
            </div>

Browser other questions tagged

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