请记住,这最初不是我的代码,因此对我来说并不是很清楚,并且非常欢迎帮助,因为我迷路了。
我想显示另一个表中的行,但我遇到了一些问题。 当我使用时
在 Controller 中:
$companies = $this->Users->Companies->find('list');
在 View 中
echo $this->Form->input('companies._ids',['options' =>$companies,'multiple'=>'checkbox']);?>
我在这些基本复选框中获取了公司的所有表 ID 和名称。
但是,因为我想使用每一行而不仅仅是 id 和名称,所以我尝试使用:
$companies = $this->Users->Companies->find('all');
但这会引发两个问题: 我的数据显示在这样的数组中:
{ "id": 2, "name": "Smith", "city": "Duisburg"}
第二个问题:它跳过第一行!我有 id 1 的内容,在使用 find('all')
时不显示,但在使用 find('list')
那么我怎样才能获取每个值并按照我想要的方式显示它们?
最佳答案
如果您只想列表中的不同字段,您可以指定。
$query = $this->Users->Companies->find('list', [
'keyField' => 'slug',
'valueField' => 'title'
]);
$data = $query->toArray();
// Data now looks like
$data = [
'first-post' => 'First post',
'second-article-i-wrote' => 'Second article I wrote',
];
关于mysql - 蛋糕php : echo rows from an array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44589682/