What I am trying to do is populate the category_id field in a drop down list in a create form that pulls all the categories from the tbl_category table. I looked up the code for this and added the following code in the public function actionCreate() located in the content controller:
public function actionCreate()
{
$model=new Content;
// retrieve the category model from db
$categorymodel = Category::model()->findAll(
array('order' => 'description'));
// format list as $key=>$value with listData
$categorylist = CHtml::listData($categorymodel,
'id', 'description');
[/code]
In the view _form, I then reference the $categorylist as my key=>value pairs to populate the dropdown:
<?php echo CHtml::dropDownList('category_id', $select, $categorylist, array('empty' => '(Select a category'));?>
I am getting the following error when I try and access the form:
"Invalid argument supplied for foreach()"
Can someone tell me what I am doing wrong here. Are my naming conventions incorrect?
Thanks for the help. Yes, it was the select not being assigned. In any case, I added the following function in the content model and it works perfectly:
public function getCategories()
{
//this function returns the list of categories to use in a dropdown
return CHtml::listData(Category::model()->findAll(), 'id', 'description');
}
Hello there im having a problem creating a dynamic drop down list, i’ve tried this method and it does create a drop down list but when submitting its giving an error that categories cannot be blank can someone help what am i getting wrong?