listData for more attrs

maybe somebody find it usefull

in helpers dir: UHTml.php

<?php

class UHtml {

public function listData($model, $value, $attrs)


{


	$a = array();


	foreach ($model as $field) {


		foreach (explode(&#039;,&#039;, $attrs) as $attr) {


			$a[$field[$value]] .= $field[$attr].&quot; &quot;;


		}


	}


	return $a;


}	

 

}

use:

<?php echo CHtml::activeDropDownList($model, 'model_id', UHtml::listData(Model::model()->findAll(), 'id', 'attr1,attr2,attr3')); ?>

cheers

include(UHtml.php) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory

you need to save file ./protected/components/UHtml.php

and then include it via your config

'import'=&gt;array(


	'application.models.*',


	'application.components.*',


),

Thanks RusAlex… that problem solved… But that $a[$field[$value]] .= $field[$attr]." "; has some offset issue … undefined offset…

Kazio ,

we need to rewrite that code to

public static function listName($model, $value, $attrs)

	{


	&#036;a = array();


	foreach (&#036;model as &#036;field) 


	{


	&#036;a[&#036;field[&#036;value]]=NULL;


	foreach (explode(',', &#036;attrs) as &#036;attr) 


	{


	&#036;a[&#036;field[&#036;value]] .= &#036;field[&#036;attr].&quot; &quot;;


	}


	}


	return &#036;a;


	}

Declare $a[$field[$value]]=NULL;

Otherwise there is a undefined offset problem

RusAlex, I solved that issue.

and we can solve this multiple attribute issue with CHtml::listData , By writing a function in the model

In Model




public function getJoin()

        {

                return $this->first_name  . ' ' . $this->last_name;

        }




and in view call





CHtml::listData(Model::model()->findAll(), 'id', 'Join')




Elegant and useful, thank so much.