如何在zend Framework中使用复选框显示数据


How to show data with checkboxes in zend Framework

我使用的是Zend框架。在这篇文章中,我创建了一个model,并将我的数据库连接放在这个模型中。这是我到目前为止的代码:-

public function getTagusers(){
    try {
        $stat = $this->db->query("select a.tagCode child, b.tagCode parent " .
                                 "from tag a, tag b where a.tagParentId=b.tagId");
        $aResultData = $stat->fetchall();   
    }
    catch(Exception $e){
        error_log('Exception in '.__FUNCTION__.' : line '.__LINE__.' : '
                  . $e->getMessage());  
    }  
    return $aResultData;
}

现在我在控制器中使用动作。到目前为止,我的代码是:-

public function listAction()
{
    $tagusers =new Admin_Model_DbTable_Tagusers();
    $this->view->taguser =$tagusers->fetchall();
}

现在,最后我想在视图list.html中回显我的数据。到目前为止,我的代码是:-

<script>
<!-- Begin
    function Check(chk)
    {
        if(document.myform.Check_ctr.checked==true){
            for (i = 0; i < chk.length; i++)
                chk[i].checked = true ;
        } else {
            for (i = 0; i < chk.length; i++)
                chk[i].checked = false ;
        }
    }
// End -->
</script>
<?php foreach($this->taguser as $taguser) ?>
    <form name="myform" action="checkboxes.asp" method="post">
        <b>Select Allowed keywords below:</b><br>
        <input type="checkbox" name="Check_ctr" value="yes"
         onClick="Check(document.myform.check_list)"><b>Select all keywords</b>
        <br>
        <input type="checkbox" name="check_list" value="1">
            <?php echo $this->escape($taguser->tagCode);?><br>
        <input type="checkbox" name="check_list" value="2">
            <?php echo $this->escape($taguser->tagParentId);?><br>
    </form>

但我无法正确地回显数据。有人能根据我的查询向我解释我能做些什么来回复结果吗。

首先,如果您使用Zend Framework作为出现的框架。。。您的第一个错误是viewscript具有.phtml扩展名是正常的(我知道您可能已经更改了这一点)。

接下来你的php不正确:

<?php foreach($this->taguser as $taguser): //need to colon for alternate loop syntax ?>
    <form name="myform" action="checkboxes.asp" method="post">
        <b>Select Allowed keywords below:</b><br>
        <input type="checkbox" name="Check_ctr" value="yes"
         onClick="Check(document.myform.check_list)"><b>Select all keywords</b>
        <br>
        <input type="checkbox" name="check_list" value="1">
            <?php echo $this->escape($taguser->tagCode); 
           //if this causes errors use
           //array notation $taguser['tagCode']?><br>
        <input type="checkbox" name="check_list" value="2">
            <?php echo $this->escape($taguser->tagParentId);?><br>
    </form>
<?php endforeach //need to end the foreach statement alternate syntax?>

我不会批评你的表格,如果你想为每一张唱片建立一个单独的表格,那是你的事。

好吧,您正在打印N个表单(其中N=count($this->taguser)),每个表单包含3个具有相同值的复选框(分别为'yes'、'1'和'2'),这根本没有意义。

如果我是对的,你的表格应该是这样的:

<form name="myform" action="checkboxes.asp" method="post">
    <b>Select Allowed keywords below:</b><br>
    <input type="checkbox" name="Check_ctr" value="yes"
     onClick="Check(document.myform.check_list)"><b>Select all keywords</b>
    <br>
<?php foreach($this->taguser as $taguser): ?>
    <input type="checkbox" name="check_list" value="<?php echo $this->escape($taguser->tagCode);?>">
        <br>
    <input type="checkbox" name="check_list" value="<?php echo $this->escape($taguser->tagParentId);?>">
        <br>
<?php endforeach; ?>

不过,您应该阅读Zend_Form。一开始很难理解,但这完全值得。