Groups of checkboxs

Hello i need a help printing a list of checkboxes divided by groups.

I got a model that corresponds to Project, and every project may have properties and that properties are divided by categories.

I got this relations:

Project model:

‘properties’ => array(self::MANY_MANY, ‘Property’, ‘project_property(id_project, id_property)’),

Property model:

‘projects’ => array(self::MANY_MANY, ‘Project’, ‘project_property(id_project, id_property)’),

‘idCategory0’ => array(self::BELONGS_TO, ‘Category’, ‘id_category’),

Category model:

‘properties’ => array(self::HAS_MANY, ‘Property’, ‘id_category’),

I’m using this code on my Project _form (create/update) view:

echo CHtml::activeCheckboxList(

$model, 'properties',


CHtml::listData(Property::model()->findAll(), 'id_property', 'name'),


array('template'=>'{input} {label}','object'=>'id_property')


);

and it works nice but it prints like this:

[] HTML

[] CSS

[] PHP

[] JAVA

[] C++

But i need checkboxes divided in categories like this:

I want to display this:

Webdesign:

[] HTML

[] CSS

Programming Languages:

[] PHP

[] JAVA

[] C++

Please help me… thanks.

you can try something like that:




foreach (Category::model()->findAll as $category)

	echo CHtml::activeCheckboxList(

		$model, 

		'properties',

		CHtml::listData(Property::model()->findAll("id_category={$category->id}"), 'id_property', 'name'),

		array('template'=>'{input} {label}','object'=>'id_property'));



Using




foreach (Category::model()->findAll() as $category)

{

    echo "<br/>".$category->name."<br/><br/>";

    echo CHtml::activeCheckboxList($model,'properties',

    CHtml::listData(Property::model()->findAll("id_category={$category->id_category}"),'id_property','name'),

    array('template'=>'{input} {label}','object'=>'id_property'));

}



It returns empty array of ‘properties’ when POSTing. Any help plz??

Sorry, koz, yesterday I was distracted and didn’t read carefully your problem.

If you have to create some input in a foreach, you should collect a tabular input in order do know from wich input arrive every data.

This is not a simple task, because is a bit strange that there are MANY input for a single database field.

You can try to do like that:




 CHtml::activeCheckboxList($model,"[{$category->id_category}]properties",

    CHtml::listData(Property::model()->findAll("id_category={$category->id_category}"),'id_property','name'),

    array('template'=>'{input} {label}','object'=>'id_property'));



Adding [{$category->id_category}]properties will made so in post you will receive an array with all submitted vaues. Then you have to work on this array for create the array to save in database.

I never do like that, because I don’t like to save array or strange stuffs in database. For this kind of problems, instead of a single field ‘properties’ I create a table ‘project_has_propery’, and I create lot of checkbox as I want, then I use the post value for add/delete rows in the third table.

Just wondering, how will you find all project with programming language php? Better to have a third table, I think.

Ok i had a third table named project_has_property but i had to change my system do support multi-language so now i got another 2 tables:

project_lang

project_lang_property

project_lang is the project translations

and project_lang_property is like the old project_had_property but now i have another field in PK (id_language)

And i resolved my problem using a simple checkboxlist

what you think about the solution?

I got this code on my view updade/create _form:




<?php

$categories = CHtml::listData(Category::model()->findAllByAttributes(array('id_language'=>$modellang->id_language)), 'id_category', 'name');


$selection = CHtml::listData(

			ProjectLangProperty::model()->findAllByAttributes(array('id_project'=>$model->id_project,'id_language'=>$modellang->id_language)), 

			'id_property', 'id_property'

			);


foreach ($categories as $idCategory => $nameCategory)

{

	$properties = Property::model()->findAllByAttributes(array('id_category'=>$idCategory));

	if($properties!=null) echo "<p><h4>".$nameCategory."</h4></p>";


	echo CHtml::CheckboxList(

		'props', $selection,

		CHtml::listData($properties, 'id_property', 'name')

	);

}

?>



I don’t know if this is the best aproach… neither if the best practices putting all this code on the view. What you think?