How to show data with checkboxes in zend Framework

2019-09-03 14:13发布

I am using Zend framework. In this i create a model and put my database connection in this model. Here is my code so far :-

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;
}

Now I am using action in controller. My code is so far :-

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

Now finally i want to echo my data in view list.html. My code is so far :-

<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>

But I am not able to echo the data properly. Can anyone explain me what I can do to echo the result according to my query.

2条回答
我命由我不由天
2楼-- · 2019-09-03 14:47

first if you using Zend Framework as a framework as it appears... your first error is it normal for viewscripts to have the .phtml extension (i know you may have changed this).

next your php is incorrect:

<?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?>

I'm not going to critique your form, if you want built a separate form for each record that's your business.

查看更多
SAY GOODBYE
3楼-- · 2019-09-03 15:00

Well, you're printing N forms (where N = count($this->taguser)), each one containing 3 checkboxes with the same value ('yes', '1' and '2', respectively), which makes no sense at all.

If I'm correct, your form should look like this:

<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; ?>

Still, you should read about Zend_Form. It will be hard to understand it in the beginning, but it's totally worth it.

查看更多
登录 后发表回答