Foreach with database records

2

In my database I have a table called cadastroatendo within this table I put the days of service. I'm retrieving the data through foreach ... which in turn 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 "checked" if codCadastroAtendoCat is equal to the id displayed in the listing. Done 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 to: When the database is registered in the database table, with that Profile, the field is marked as "checked". Can anyone help me?

Explanation:

I have checkbox fields, which are previously registered in the database. I can not see if there is a record in the database, to put it as checked, at the time to edit.

    
asked by anonymous 10.07.2015 / 21:49

2 answers

2

I think this should resolve.

Basically, at each iteration of the atendo array you have to check if the Id of this element exists within the array of your list of queries, and with that to say whether it will have the checked property 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>
    
10.07.2015 / 22:55
0

Your response was valid. I worked out another way too, which 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>
    
10.07.2015 / 23:13