mercredi 26 août 2015

How to set checkbox as checked by testing array result against another array?

I wanted to display checkbox as checked by testing one array from query result and another one also from a query result, but I can't seem to get it...

Here's what I've come up with so far:

first query:

$getbficiaryres = mysqli_query($link, "
SELECT
    a.`id`,
    b.`id` as `bfryid`, b.`bficiaryname`,
    c.`notes` as `bfrynotes`
FROM 
    `activity` a, `beneficiary` b, `bficiarynotes` c, `actbficiary_link` d 
WHERE
    a.`id` = c.`activity_id` AND
    a.`id` = d.`activity_id` AND
    b.`id` = c.`beneficiary_id` AND
    b.`id` = d.`beneficiary_id` AND
    a.`id` = '".$edit."'");

$getbficiaryrow = mysqli_fetch_array($getbficiaryres);

first query result:

id  bfryid  bficiaryname bfrynotes
1   2       xxx          xxx notes
1   4       yyy          yyy notes 

second query:

$bficiaryres = mysqli_query($link, "SELECT `id`, `bficiaryname` FROM `beneficiary`");

second query result:

id  bficiaryname
1   aaa
2   xxx
3   bbb
4   yyy

php codes:

$i = 0;
while($bficiaryrow = mysqli_fetch_array($bficiaryres)){
    if($bficiaryrow['id'] == $getbficiaryrow['bfryid']){    
        echo '
        <div class="form-group">
            <div class="col-sm-12">
                <div class="checkbox">
                    <label for="checkbox"><input type="checkbox" name="pmanfaat['.$i.']" value="'.$getbficiaryrow['bfryid'].'" checked />'.$getbficiaryrow['bfryname'].'
                    </label>
                </div><textarea name="ketpmanfaat['.$i.']" class="form-control">'.$getbficiaryrow['bfrynotes'].'</textarea>
            </div>
        </div>';

    }else{
        echo '
        <div class="form-group">
            <div class="col-sm-12">
                <div class="checkbox">
                    <label for="checkbox"><input type="checkbox" name="pmanfaat['.$i.']" value="'.$bficiaryrow['id'].'" />'.$bficiaryrow['bficiaryname'].'
                    </label>
                </div><textarea name="ketpmanfaat['.$i.']" class="form-control"></textarea>
            </div>
        </div>';    
    }                       
    $i++;
}

The result that I'm expecting was to get two checkboxes xxx (id no 2)and yyy (id no 4) set as checked, but the only thing that was checked is the xxx.

How do I loop properly to get all the arrays tested?

I hope my question is clear enough.

My head' about to blow... Any help is really appreciated.

Thank you.




Aucun commentaire:

Enregistrer un commentaire