PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 1, 2022

[FIXED] How to set the first selector value to 1 in Cake PHP Form->select

 January 01, 2022     cakephp, cakephp-2.0, cakephp-3.0, php     No comments   

Issue

I am a new user to Cake PHP and I am going through the cake book of Cake PHP. When I am using the $this-Form->select('field', $array); the first value is always set to 0. I want this that the first selector has a value of 0.

$this->Form->select(
    'field',
    [1, 2, 3, 4, 5]
);
Output:

<select name="field">
    <option value="0">1</option>
    <option value="1">2</option>
    <option value="2">3</option>
    <option value="3">4</option>
    <option value="4">5</option>
</select>

Can anyone help me with this query?


Solution

That's how arrays are created by default, if you don't supply keys. To use the same values for keys and values, try this:

$values = [1, 2, 3, 4, 5];
$this->Form->select(
    'field',
    array_combine($values, $values)
);


Answered By - Greg Schmidt
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing