0

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

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Wonderful! seems fine but little issue (may be due to the way codeigniter label function works). It is wrapping label text with " double quote "No Index" so click has not effect.
My bad, there is no need to define attributes for label to add for. $id is for the for attribute. I have removed the parameter and works fine. Just wonder why it is wrapping with double quotes!?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.