我正在使用zend Framework Form, 我是zend Framework中的新手,我想显示我的复选框表单: -
*SK336
*CP
*PES
*JCP
*BGH
*SK996
*KO
*RTY
*HGR
*SK547
*GPK
*SK478
*JUP
注意: - *是这里的复选框
我正在尝试的是: -public function init()
{
$parents = array();
$childs = array();
foreach ($this->Tagkey as $aResultDataValue) {
$parents [$aResultDataValue['parent']] = $aResultDataValue['parent'];
$childs [$aResultDataValue['parent']][] = $aResultDataValue['child'];
}
foreach ($parents as $parent){ // print_r ($parents); die();
$tags = new Zend_Form_SubForm();
$tags->addElements(array(
new Zend_Form_Element_MultiCheckbox('parent', array(
'multiOptions' => array($parent),
'filters' => array('StringTrim'),
'validators' => array(
array('InArray',
false,
array($parent))
)
)),
));
foreach ($childs as $child){
$tags->addElements(array(
new Zend_Form_Element_MultiCheckbox('child', array(
'multiOptions' => array($child),
'filters' => array('StringTrim'),
'validators' => array(
array('InArray',
false,
$child)
)
)),
));
}
$this->addSubForms(array(
'tags' => $tags,
)
);
}
我能够在任何.php
页面中创建这种类型的结构,但现在无法在zend框架表单中创建,我在这里使用zend子表单。
当我使用此查询时,我现在也收到了错误
警告:htmlspecialchars()要求参数1为字符串,数组在第905行的/var/www/dashboard_campaign/library/Zend/View/Abstract.php中给出
More Information
关于我的问题: -
(1)mysql qyery
select b.tagCode parent,a.tagCode child from tag a, tag b where a.tagParentId=b.tagId
(2)Zend_Debug :: dump的输出($ this-> Tagkey);
array(9) {
[0] => array(2) {
["parent"] => string(5) "SK336"
["child"] => string(2) "CP"
}
[1] => array(2) {
["parent"] => string(5) "SK336"
["child"] => string(3) "PES"
}
[2] => array(2) {
["parent"] => string(5) "SK336"
["child"] => string(3) "JCP"
}
[3] => array(2) {
["parent"] => string(5) "SK996"
["child"] => string(2) "KO"
}
[4] => array(2) {
["parent"] => string(5) "SK996"
["child"] => string(3) "RTY"
}
[5] => array(2) {
["parent"] => string(5) "SK996"
["child"] => string(3) "HGR"
}
[6] => array(2) {
["parent"] => string(5) "SK547"
["child"] => string(3) "GPK"
}
[7] => array(2) {
["parent"] => string(5) "SK478"
["child"] => string(3) "JUP"
}
[8] => array(2) {
["parent"] => string(5) "SK336"
["child"] => string(3) "BGH"
}
}
答案 0 :(得分:1)
现在我可以理解你的问题。我认为从子形式思考是难以处理的。尝试按以下方式使用zend视图脚本。
your form.php
public function init()
{
foreach ($parents as $parent) {
$parent = new Zend_Form_Element_Hidden($parent);
$parent->setDecorators(array(
array(
'ViewScript',
array(
'viewScript' => 'customviewscripts/parent.phtml',
'parent' => $parent
)
)
);
$this->addElement($parent);
}
}
文件位于views / script / customviewscript / parent.phtml
<?php
$params = $this->element->getDecorator('ViewScript')->getOptions();
$parent = $parems['parent'];
$string = '<label>$parent['name']</label><input type="check" name="parent[]">';
foreach ($children as $child) {
$string .= <label>$child['name']</label>
. <input type="check" name=child[$parent[id]][]>' ;
}
print $string;
?>
这不是真正的解决方案。我只是例子。我认为你可以自定义。大多数开发人员使用视图脚本来制作复杂的表单。
答案 1 :(得分:0)
你看起来提到多选项约翰的错误语法,你应该试试这个。
删除$ parent的数组,请参阅下面的示例。
new Zend_Form_Element_MultiCheckbox('parent', array(
'multiOptions' => $parent,
'filters' => array('StringTrim'),
'validators' => array(
array('InArray',
false,
array($parent))
)
从db结果中,您必须为$ parent变量生成以下类型的数组
$ parent变量应该像这样复制这个数组并尝试自己而不从数据库中获取,你会看到 所有选项
$parent=Array ([1] => blah1 [2] => blah2 [3] => blah3 [4] => blah4 [5] => blah5);
检查这个也是多个复选框,代替数组你应该尝试放置数组变量,我没试过这只是看着互联网,但应该工作正常。
$category1 = new Zend_Form_Element_MultiCheckbox('categories',Array())
$category1->setLabel('Category 1');
$category2 = new Zend_Form_Element_MultiCheckbox('categories',Array())
$category2->setLabel('Category 2');
...稍后......
$this->addElement($category1)
->addElement($category2);