Php 如果输入值=1,则选中Zend Framework 2复选框

Php 如果输入值=1,则选中Zend Framework 2复选框,php,checkbox,zend-framework2,Php,Checkbox,Zend Framework2,我正在做一个基于的新模块,我有一个问题。如果值=1,如何选中复选框 查看/索引.phtml <?php $title = 'Newsletter'; $this->headTitle($title); ?> <h1>Newsletter</h1> <p>Select users who are to receive the newsletter.</p> <table class="table table-stripe

我正在做一个基于的新模块,我有一个问题。如果值=1,如何选中复选框

查看/索引.phtml

<?php

$title = 'Newsletter';
$this->headTitle($title);
?>

<h1>Newsletter</h1>
<p>Select users who are to receive the newsletter.</p>

<table class="table table-striped">
    <tr>
        <td width="80%"><strong>User_id</strong></td>
        <td width="10%" align="center"><strong>Yes/No</strong></td>
    </tr>
    <?php foreach ($newsletters as $newsletter) : ?>

        <tr>
            <td><?php echo $this->escapeHtml($newsletter->user_id); ?></td>
            <td align="center"><?php echo $this->escapeHtml($newsletter->wantNewsletter); ?> <input type="checkbox" name="wantNewsletter" value="<?php echo $this->escapeHtml($newsletter->wantNewsletter); ?>"></td>
        </tr>

    <?php endforeach; ?>
    <tr>
        <td colspan="2" align="right"><button>Zapisz</button></td>
    </tr>
</table>

新闻稿
选择要接收新闻稿的用户

用户id 是/否
您不必担心选中复选框。它将自动为您完成,但为了使其工作,您必须先调整它

首先,您需要在表单中定义复选框:

final class SomeForm extends Form
{

    public function __construct($name = null)
    {
        .....

        // Add the checkbox here
        $this->add(array(
            'name' => 'wantNewsletter',
            'type' => 'checkbox',
        ));
然后在模板中,您可以简单地渲染它:

 echo $this->formRow($form->get('wantNewsletter'));
如果属性
wantNewsletter
的值为
1
,则复选框将自动选中

您拥有的另一种解决方案实际上是此任务的一种变通方法,只需在值为
1
时渲染属性即可

<input type="checkbox" name="wantNewsletter" value="<?php echo $newsletter->wantNewsletter); ?>" <?php echo $newsletter->wantNewsletter ? 'checked' : ''; ?>>

您不必担心选中复选框。它将自动为您完成,但为了使其工作,您必须先调整它

首先,您需要在表单中定义复选框:

final class SomeForm extends Form
{

    public function __construct($name = null)
    {
        .....

        // Add the checkbox here
        $this->add(array(
            'name' => 'wantNewsletter',
            'type' => 'checkbox',
        ));
然后在模板中,您可以简单地渲染它:

 echo $this->formRow($form->get('wantNewsletter'));
如果属性
wantNewsletter
的值为
1
,则复选框将自动选中

您拥有的另一种解决方案实际上是此任务的一种变通方法,只需在值为
1
时渲染属性即可

<input type="checkbox" name="wantNewsletter" value="<?php echo $newsletter->wantNewsletter); ?>" <?php echo $newsletter->wantNewsletter ? 'checked' : ''; ?>>