[SOLVED] Syntax Help, CGridView and checkbox

Hi, I have a custom checkbox in a CGridView column in one of my views. Without going into too much detail I can’t use relations to solve the problem.

I have two tables, X which has a model and is used to render the CGridView, and Y which doesn’t have a model or controller. There is a foreign key in table Y that ties to X.id. The custom checkbox is used to “copy” that row into table Y (with code in X.controller) and if it is already checked then it indicates that that row already exists in table Y.

Before the grid is created I have code that populates an array with the array keys created with the foreign key and some text strings to create a unique id for the checkbox.

The following code currently generates this error:




'value'=>'CHtml::checkBox(

    "cid_'.$record->name.'_$data->id",

    (array_key_exists("cid_'.$record->name.'_$data->id",'.$selectedRecords.')) ? "checked" : null,

    array("value"=>$data->id,"id"=>"cid_'.$record->name.'_".$data->id)

)',



I’m thinking that this is just something with the quoting as the code in the core component that is throwing this error is:


return eval('return '.$_expression_.';');

But I just can’t see where, I think I’ve been looking at it too long.

Thanks,

Does this work?




'value'=>'CHtml::checkBox(

    "cid_'.$record->name.'_$data->id",

    (array_key_exists("cid_'.$record->name.'_$data->id",'.$selectedRecords.')) ? "checked" : null,

    array("value"=>$data->id,"id"=>"cid_'.$record->name.'_$data->id")

)',



/Tommy

Thanks, I’ve definitely been looking at it too long if I didn’t see that missing double quote on line 4. :rolleyes:

It’s not completely working though, now it is giving an error on:

Which is the same eval line in CComponent.php.

Thanks,

‘value’=>'CHtml::checkBox(

"cid_'.$record->name.'_$data->id",


(array_key_exists("cid_'.$record->name.'_$data->id",'.$selectedRecords.')) ? "checked" : null,


array("value"=>$data->id,"id"=>"cid_'.$record->name.'_$data->id")

)’,

you cant do this because CHtml::checkBox() i defined method, any you cant call function array_key_exists

in method function parameters

CHtml::checkBox() is defined




public static function checkBox($name,$checked=false,$htmlOptions=array())

{

    if($checked)

        $htmlOptions['checked']='checked';

    else

        unset($htmlOptions['checked']);

    $value=isset($htmlOptions['value']) ? $htmlOptions['value'] : 1;

    self::clientChange('click',$htmlOptions);


    if(array_key_exists('uncheckValue',$htmlOptions))

    {

        $uncheck=$htmlOptions['uncheckValue'];

        unset($htmlOptions['uncheckValue']);

    }

    else

        $uncheck=null;


    if($uncheck!==null)

    {

        // add a hidden field so that if the radio button is not selected, it still submits a value

        if(isset($htmlOptions['id']) && $htmlOptions['id']!==false)

            $uncheckOptions=array('id'=>self::ID_PREFIX.$htmlOptions['id']);

        else

            $uncheckOptions=array();

        $hidden=self::hiddenField($name,$uncheck,$uncheckOptions);

    }

    else

        $hidden='';


    // add a hidden field so that if the checkbox  is not selected, it still submits a value

    return $hidden . self::inputField('checkbox',$name,$value,$htmlOptions);

} 




give me all your code of your issue to help you

Ok, that makes sense, thanks.

My solution was to loop through the array and create a string that represented the array (ie: array("cid_testRow_1"=>1)). Then I overrode CHtml::checkBox converting the "checked" value to an array. Then because the id and name parameters of the checkbox are the same, I could use array_key_exists to compare the already passed in $name value against the array.

In the view:




'value'=>'CHtml::checkBox(

    "cid_'.$record->name.'_$data->id",

    '.$stringArray.',

    array("value"=>$data->id,"id"=>"cid_'.$record->name.'_$data->id")

)',



In the overridden MyHtml::checkBox:




public static function checkBox($name,$checkedArray=array(),$htmlOptions=array())

{

	if (array_key_exists($name,$checkedArray))

	{

		$checked = true;

	}

	else

	{

		$checked = false;

	}

...