0
echo $this->Form->input('quantity', array('options' => array('1','2','3','4')));

I have this code to input the values 1,2,3 or 4 into the quantity field in my database.

If a user selects 1 it inputs 0, 2 inputs 1 etc.

If the code is

echo $this->Form->input('quantity');

The user can input what they like and guess what... It works. What am I doing wrong?

1 Answer 1

1

I think you're trying to implement a drop-down box. I think you can try this:

$options = array('1' => '1', '2' => '2', '3' => '3', '4' => '4');
echo $this->Form->input('quantity', array('options' => $options, 'default' => '1'));

this will create a select drop down box.

For detail please see here.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. This worked. I had drop down box, but it thought each number was 1 less. Does the '1'=>'1' etc fix it?
yes. if you put only options array then it will index those array item like normal array mechanism. But for your custom indexing you need to set options as associative array (key => value) pair.
The difference with the original code, is that CakePHP uses the 'keys' (indexes) of the array as values and the 'values' as visible labels; array(1,2,3); is equal to array(0 => 1, 1 => 2, 2 => 3); i.e. the value of 1 is 0
@BlairMurray welcome.. actually we all are learning every moment.

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.