【问题标题】:How to show data with checkboxes in zend Framework如何在 zend 框架中使用复选框显示数据
【发布时间】:2012-01-27 18:20:49
【问题描述】:

我正在使用 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>

但我无法正确回显数据。谁能根据我的查询向我解释我可以做些什么来回显结果。

【问题讨论】:

  • 对于这么多信息,我们几乎无法帮助您。你得到了什么输出,你期望得到什么?

标签: php zend-framework


【解决方案1】:

首先,如果您使用 Zend Framework 作为框架,因为它看起来...您的第一个错误是 viewscripts 具有 .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?>

如果您想为您的业务的每条记录构建单独的表单,我不会批评您的表单。

【讨论】:

    【解决方案2】:

    好吧,您正在打印 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 的信息。一开始会很难理解,但完全值得。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      相关资源
      最近更新 更多