dongwuxie7976 2014-02-04 11:21
浏览 30
已采纳

Codeigniter:使用数组填充多个复选框和标签

In Codeigniter I am trying to populate 4 checkboxes for site options in this way

<label for="noindex"><input type="checkbox" id="noindex" value="noindex"  class="" />No Index</label>
<label for="nofollow"><input type="checkbox" id="nofollow" value="nofollow"  class="" />No Follow</label>
.....and so on

I want to use array to populate instead of writing code for each. Here I am trying using array

<div class="form-group">
    <?= form_label('Search Engine', 'check', array('class' => 'col-sm-4 control-label')); ?>
    <div class="col-sm-6">              
    <?php
    $checkboxes = array('noindex', 'nofollow', 'noarchive', 'nosnippet');
    foreach($checkboxes as $check):
        $data = array(
        'name' => $check,
        'id' => $check,
        'value' => $check,
        'class' => '',                      
        );

        $labels_text = array('No Index', 'No Follow', 'No Archive', 'No Snippet');
        //print_r($check);
        echo '<div class="checkbox">';
        foreach ($labels_text as $label_text):
        echo form_label(form_checkbox($data) . $label_text, $check.'-label', array('for' => $check));
        endforeach;
        //echo form_checkbox($data);
        echo '</div>';
    endforeach;
    ?>
    </div>
</div>

Here I stuck with the labels. Where I want to display different label (of course) for each checkbox.

Can anyone help me to get different label for each?

  • 写回答

1条回答 默认 最新

  • doumu6997 2014-02-04 11:39
    关注

    Try like this:

    $checkboxes = array('noindex'=>'No Index', 'nofollow'=>'No Follow','noarchive'=>'No Archive', 'nosnippet'=>'No Snippet');
    
    foreach($checkboxes as $check=>$label_text):
      $data = array(
            'name' => $check,
            'id' => $check,
            'value' => $check,
            'class' => '',                      
            );   
    
            echo '<div class="checkbox">';
    
            echo form_label(form_checkbox($data) . $label_text, $check.'-label', array('for' => $check));
    
            echo '</div>';
        endforeach;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 加一个K2暂停音乐的功能
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验