How to fit an 8 column class into a 2 column input? [Solved]

Hello,
Another post about my first PHP project. I am sure my question is simple to answer for others, but I haven’t found the answer so far.
I have a class with 8 attributes (ID, name, and 6 integers). So far, I have the following code to retrieve the data from the database, but I understand it tries
$countries = $this->Countries->find();
I want a select using ID as identifier and name as the label. For instance;
<OPTION ID="1">Algeria</OPTION>.
I can’t find detailed documentation about the Form->input function to know how to get my IDs into the ‘value’ attribute of OPTION and the names displayed to the user between the OPTION tags. I managed to create the following code.
<?php echo $this->Form->input('Home country', array('options' => $countries)); ?>
But I see the entire unformatted record ending up in the display, starting with the first option that is automatically assigned value 0. I have seen documentation about HTML but not sure how to apply this here or if that is relevant.

Name{
“ID”: 1,
“name”: “Albania”,
“PDI”: 90,
“IDV”: 20,
“MAS”: 80,
“UAI”: 70,
“LTO”: 61,
“IND”: 15
}
Can anyone help?

Hi @lafeber, try the following

In CakePHP 3
inside APP/src/Model/Table/CountriesTable.php

public function initialize(array $config)
{
parent::initialize($config);
# some code…
$this->setDisplayField(‘name’);
# some other code…
}

CakePHP 2
In APP/src/Model/Country.php

… Some code …
public $displayField = ‘name’;
… Some other code…

Now in your controller, say APP/src/Controller/CountriesController.php

CakePHP 2
$countries = $this->Countries->find(‘list’);

CakePHP 3
$countries = $this->Countries->find(‘list’, [‘keyField’ => ‘id’, ‘valueField’ => ‘name’]);
OR
$countries = $this->Countries->find(‘list’); // For this to work you must have updated your CountriesTable.php class as I did earlier

That’s wonderful. This worked. Thank you very much for the quick reply. Now onto the next hurdle. :slight_smile:

@lafeber You are welcome…