0

I'm the newbies to cakephp. I created a checkbox form named existing_question.ctp and stored in array. However, when I tried to print_r the array comes out really weird. It comes out like this.

existing_question.ctp

<?php

/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\Question[]|\Cake\Collection\CollectionInterface 
$questions
*/
use Cake\ORM\TableRegistry;
?>
 <nav class="large-3 medium-4 columns" id="actions-sidebar">
  <ul class="side-nav">
    <li class="heading"><?= __('Actions') ?></li>
    <li><?= $this->Html->link(__('Courses'), ['controller' => 'Courses', 
  'action' => 'index']) ?></li>
</ul>
</nav>
</nav>
 <div class="questions form large-9 medium-8 columns content">
 <fieldset>
    <legend><?= __('Add Question') ?></legend>
    <table cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <th scope="col"><?= $this->Paginator->sort('checked') ?></th>
            <th scope="col"><?= $this->Paginator->sort('id') ?></th>
            <th scope="col"><?= $this->Paginator->sort('question') ?></th>
            <th scope="col"><?= $this->Paginator->sort('marks') ?></th>
        </tr>
    </thead>
    <tbody>


    <?php echo $this->Form->create($question) ?>   
        <?php foreach ($questions as $question): ?>
        <tr>
            <td><?= $this->Form->checkbox('id[].', array('value' => 
    $question['id'], 'name' => 'Question[id][]', 'checked'=>false))?></td>
            <td><?= $this->Number->format($question->id) ?></td>
            <td><?= h($question->question) ?></td>
            <td><?= $this->Number->format($question->marks) ?></td>
        </tr>
        <?php endforeach; ?>                 
    </tbody>
    </table>
    <input type="submit" value="Save">        
    </form>
    <div class="paginator">
    <ul class="pagination">

        <?= $this->Paginator->first('<< ' . __('first')) ?>
        <?= $this->Paginator->prev('< ' . __('previous')) ?>
        <?= $this->Paginator->numbers() ?>
        <?= $this->Paginator->next(__('next') . ' >') ?>
        <?= $this->Paginator->last(__('last') . ' >>') ?>
    </ul>
    <p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
     <?php //$this->Form->button(__('Submit'), ['action' => 'existingQuestion'])
         $this->Form->submit('Save'); 
        // $this->Form->end() ?>
</div>
</fieldset>

I tried so many tutorials but it doesnt comes out like it should. Can anyone help me???

EDITED: I want it to be stored in array like this

2
  • What is the expected results? Commented Dec 27, 2017 at 4:10
  • I have edited the post Commented Dec 27, 2017 at 5:07

1 Answer 1

1

First of all, you HTML has some problems. The form cannot be a child of the table so wrap the table by form.

As you can read the documentation it says

'hiddenField' - For checkboxes and radio buttons, by default, a hidden input element is also created, along with the main element, so that the key in $this->request->getData() will exist even without a value specified. For checkboxes its value defaults to 0 and for radio buttons to ''.

So if you inspect your code you will see something like this

<input type="hidden" name="Question[id][]" value="0">
<input type="checkbox" name="Question[id][]" value="2">

That's why you are getting some 0 values as well.

This can be disabled by setting 'hiddenField' to false:

<?= $this->Form->checkbox('id[].', array(
    'value' => $question['id'], 
    'name' => 'Question[id][]', 
    'checked' => false, 
    'hiddenField' => false
)) ?>

So the final code will look like this

<?php

/**
 * @var \App\View\AppView $this
 * @var \App\Model\Entity\Question[]|\Cake\Collection\CollectionInterface
$questions
 */

use Cake\ORM\TableRegistry;

?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
    <ul class="side-nav">
        <li class="heading"><?= __('Actions') ?></li>
        <li><?= $this->Html->link(__('Courses'), ['controller' => 'Courses',
                'action' => 'index']) ?></li>
    </ul>
</nav>
</nav>
<div class="questions form large-9 medium-8 columns content">
    <fieldset>
        <legend><?= __('Add Question') ?></legend>
        <?= $this->Form->create($question) ?>
        <table cellpadding="0" cellspacing="0">
            <thead>
            <tr>
                <th scope="col"><?= $this->Paginator->sort('checked') ?></th>
                <th scope="col"><?= $this->Paginator->sort('id') ?></th>
                <th scope="col"><?= $this->Paginator->sort('question') ?></th>
                <th scope="col"><?= $this->Paginator->sort('marks') ?></th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($questions as $question): ?>
                <tr>
                    <td><?= $this->Form->checkbox('id[].', [
                            'value' => $question['id'],
                            'name' => 'Question[id][]',
                            'checked' => false
                        ]) ?></td>
                    <td><?= $this->Number->format($question->id) ?></td>
                    <td><?= h($question->question) ?></td>
                    <td><?= $this->Number->format($question->marks) ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?= $this->Form->submit('Save') ?>
        <?= $this->Form->end() ?>
        <div class="paginator">
            <ul class="pagination">
                <?= $this->Paginator->first('<< ' . __('first')) ?>
                <?= $this->Paginator->prev('< ' . __('previous')) ?>
                <?= $this->Paginator->numbers() ?>
                <?= $this->Paginator->next(__('next') . ' >') ?>
                <?= $this->Paginator->last(__('last') . ' >>') ?>
            </ul>
            <p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
            <?php //$this->Form->button(__('Submit'), ['action' => 'existingQuestion'])
            // $this->Form->submit('Save');
            // $this->Form->end() ?>
        </div>
    </fieldset>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, may I asked your help to look at my problem [here] (stackoverflow.com/questions/47988273/…). I really appreciate if you can spend your precious time to look at that link.

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.