Yii框架 - 使用复选框过滤器

时间:2017-07-12 01:01:42

标签: php yii

我目前正在学习Yii框架。

以下是我的代码段。它是一个类别列表,数据来自api:

        <div class="cbp-l-filters-button" style="width: 75%;">
            <?php foreach ($categories as $c): ?>
                <a class="cbp-filter-item <?php echo ($category == $c->id)?'cbp-filter-item cbp-filter-item-active':'' ?>" href="<?php echo $this->createUrl('//idea/frontend/explore', array('category'=>$c->id)) ?>">
                    <?php echo $c->title; ?>
                </a>
            <?php endforeach ?>
        </div>

enter image description here

但现在我想将类别更改为如下所示的复选框:

enter image description here

但不确定如何解决这个问题。到目前为止,这是我尝试的代码,但没有运气。

      <div class="row" style='float:left;;margin-left:5px'>
          <?php foreach ($categories as $c): ?>

              <?php echo '<span for="label" style="margin-bottom:5px;font-size: 0.9em;font-weight: bold;">Label</span><br />'; ?>
              <?php echo $form->checkBox($c,'title',array('value'=>1,'uncheckValue'=>0,'checked'=>($c->id=="")?true:$c->title),'style'=>'margin-top:7px;')); ?>
              <?php echo $form->error($c,'title'); ?>

              <?php endforeach ?>
          </div>

1 个答案:

答案 0 :(得分:0)

您需要在循环中添加一个键,并将该键添加到您的属性名称中。

 <?php foreach ($categories as $key => $c): ?>
      <?php echo $form->checkBox($c, '[' . $key . ']title';?>
 <?php endforeach ?>

有关使用表格数据的信息,请参阅此文章。 http://www.yiiframework.com/doc/guide/1.1/en/form.table

相关问题