check box problem

i have a table called interest having 5 rows

i am showing in on view.with check box.

i have another table callled bloggerinteretrelation in which blogger select his interest .

My problem is that .when i login with blogger id it should show all 5 interest with check box but only those check box should be checked which blogger login.

if i have interest reading,singing,playing etc.

if blogger select reading.then remaining field should be unchecked,

how i can do this.

something like:

  1. always showing all records from interest table

  2. get all records from bloggerinterrelation for current user/blogger

    create array of interest_id’s

  3. compare 2 with one and check what is previously selected.

foreach (interestRecords as interest)

{

 if(in_array(interest->id, [blogger.interest_id's] ))


    echo checkBox(string $name, boolean $checked=true, array $htmlOptions=array());


 else


    checkBox(string $name, boolean $checked=false, array $htmlOptions=array ( ));

}

hope this will help U

it helps me

but


 <?php foreach ($data7 as $row1): ?>

                <?php foreach ($data1 as $row): ?>

                    <?php

                        if($row1['type']==$row['type'])

                        {

                            echo CHtml::checkBox('',true,array()) ;

                            echo $row1['type'];

                        }

                        else

                        {

                            echo CHtml::checkBox('',false,array()) ;

                            echo $row1['type'];

                        }

                        ?>

                <?php endforeach; ?><br>

            <?php endforeach; ?>

it is printing whole check box two times

Hello!

What data has your $data7 variable? And your $data1?

You should load all "interest" rows in $data7 then load "bloggerinteretrelation" for actual member in $data1, can you put here the print_r() of $data7 and $data1?

Anyway, if the interests you wanna show are only for a single blogger, i think you should do the logic in the controller so you only have to print it in your view, creating model instance like this to send it to the view




$searchCriteria = new CDbCriteria(); 

        $searchCriteria->select = "t.type, bir.blogger_id";

        $searchCriteria->join = "LEFT JOIN bloggerinteretrelation bir ON t.id = bir.interest_id WHERE bir.blogger_id = ".$blogger_id;

        $data1 = Interest::model()->findAll($searchCriteria);



and in the view just:




<?php  

foreach ($data1 as $row):

            if ($row["blogger_id"] != NULL)

                echo CHtml::checkBox('',true,array()) ;

            else

                echo CHtml::checkBox('',false,array()) ;

            echo $row["type"];

        endforeach;

?>

<br>