gpt4 book ai didi

php - Cakephp 2 在多个部分获取数据的问题

转载 作者:行者123 更新时间:2023-11-30 22:00:16 25 4
gpt4 key购买 nike

我已将数据存储在两个表的多选选项中。

表格:用户:

id      user_name  status
1 test 1
2 test2 1
3 test3 1
4 test4 1

爱好:

id      hobby_name
1 Cricket
2 Movie
3 Football
4 Music
5 Dance

我已经保存了如下数据:

id      user_id  hobby_id
1 1 5
2 1 19
3 2 17
4 2 18

每个用户都有多种爱好。现在,当我编辑 user_id 1 时,我想在多个选择框中显示所选的 ID。

我已经在 UsersCroller.php 中获取了 hobbies 数据,如下所示:

$this->loadModel('Hobby');
$hobbies = $this->Hobby->find('list', array('fields' => array('Hobby.id', 'Hobby.hobbyname'), 'recursive' => -1));
$this->set(compact('hobbies'));

admin_add.ctp 中创建了下拉选择框,如下所示:

<?php echo $this->Form->input('hobbies', array("label" => false, 'multiple' => 'multiple', 'type' => 'select', 'options' => $hobbies, "empty"=>"Select Hobbies", "div" => false, "class" => "m-wrap span12 validate[required]", "data-errormessage-value-missing" => "Please select the hobbies!" )); ?>

如何在编辑和显示多选时获取这些记录。

最佳答案

您可以尝试在下面的表单输入并传递选定的 ID 和所有爱好。

    $selected = array(5, 19);
$options = array(5, 19, 17, 18);

echo $this->Form->input('Hobbies', array('multiple' => true, 'options' => $options, 'selected' => $selected));

关于php - Cakephp 2 在多个部分获取数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43603612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com