我现在有了以下php脚本,我想把他的脚本放在zend格式中。这是我到目前为止的代码:-
$parents = array();
$childs = array();
foreach ($this->Tagkey as $aResultDataValue) {
$parents [$aResultDataValue['parent']] = $aResultDataValue['parent'];
$childs [$aResultDataValue['parent']][] = $aResultDataValue['child'];
}
foreach ($parents as $parent) {
echo '<div>';
$parent_value = "'$parent'";
echo '<div><input type="checkbox" name="parents[]" value="'.$parent.'" id="'.$parent.'" class="parentCheck"/>
<label for="parents_'.$parent.'">'.$parent.'</label></div>';
foreach ($childs[$parent] as $child) {
$child_value = "'$child'";
echo '<div style="margin-left:15px;"><input type="checkbox" name="childs[]" value="'.$child.'" id="childs_'.$child.'" class="child_'.$parent.'" onclick="checkParent('.$parent_value.','.$child_value.');"/>
<label for="childs_'.$child.'">'.$child.'</label></div>';
}
echo '</div>';
}
现在我将使用zend形式的纯php脚本,我正在尝试的是:-
类Admin_Form_Users扩展Zend_Form{
public function init()
{
$parents = array();
$childs = array();
foreach ($this->Tagkey as $result) {
$parents [$result['parent']] = $result['parent'];
$childs [$result['parent']][] = $result['child'];
}
foreach ($parents as $parent) {
$subForm = new Zend_SubForm();
$subForm->addElement($parent);
foreach ($children as $child) {
$subForm->addElement($child);
}
$form->addSubForm($subForm);
}
$parent = new Zend_Form_SubForm();
$parent->addElements(array(
new Zend_Form_Element_MultiCheckbox('subscriptions', array(
'label' =>
'Which parent would you like to subscribe to?',
'multiOptions' => $parents,
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
array('InArray',
false,
array(array_keys($parents)))
)
)),
));
$child = new Zend_Form_SubForm();
$child->addElements(array(
new Zend_Form_Element_MultiCheckbox('subscriptions', array(
'label' =>
'Which child would you like to subscribe to?',
'multiOptions' => $childs,
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
array('InArray',
false,
array(array_keys($childs)))
)
)),
));
$this->addSubForms(array(
'$child' => $child,
'parent' => $parent
));
我收到一个错误
Warning: Invalid argument supplied for foreach() in /var/www/dashboard_campaign/application/modules/admin/forms/Users.php on line 19
这里的意思是:-foreach ($this->Tagkey as $aResultDataValue) {
Tagkey
来自数据库模型
我能做什么我是zend框架中的newbie
我做错了什么帮助我
$TagKey
不是Zend_Form的成员,我不知道它被添加到了哪里。
您可以将TagKey来自的模型传递到表单的构造函数中,或者在表单的init()
方法中,您需要创建模型的实例并获取TagKey变量。
以下是如何将它从控制器中获取到表单对象中。
public function editAction()
{
$tags = new Campaign_Model_DbTable_Tag();
$aResultData = $tags->getTagkey();
$this->view->Tagkey = $aResultData;
$form = new Admin_Form_Users($aResultData);
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
// valid
} else {
// errors
}
}
}
然后在表单中添加一个构造函数。
// Admin_Form_Model
public function __construct($tagKey)
{
$this->tagKey = $tagKey;
parent::__construct(); // you must call this last as it calls init()
}
您的问题是一个简单的错误:
您设置了变量$childs = array();
然后称其为$children:
foreach ($children as $child) {
$subForm->addElement($child);
}
这可能不是这个脚本中唯一的错误,但它是消息处理的错误。