0

所以,我有一个看起来像这样的数组:

$grades =
Array(

    [0] => Array ( [Grade] => Array ( [id] => 0 [name] => Kindegarten ) ) 
    [1] => Array ( [Grade] => Array ( [id] => 1 [name] => First ) ) 
    [2] => Array ( [Grade] => Array ( [id] => 2 [name] => Second ) ) 
    [3] => Array ( [Grade] => Array ( [id] => 3 [name] => Third ) ) 
    [4] => Array ( [Grade] => Array ( [id] => 4 [name] => Fourth ) ) 
    [5] => Array ( [Grade] => Array ( [id] => 5 [name] => Fifth ) ) 

    )//End array 

我想知道有没有办法任意选择一个键(第一个0-5)?CakePHP 像这样返回我的表中的项目,我不明白如何使用选项使表单帮助程序的打印符合。

echo $this->Form->input('grade_selection', 
        array('type' => 'radio', 'options' => array($grades[?]['Grade']['id'] => $grades[?]['Grade']['name'])));

这 ?是如何让它改变选项,所以我可以得到数组中的每个项目?

class GradesController extends AppController {
    public function index(){
        //Gets all the rows in the grade table and stores it into a variable called grade. 
        //$grades = $this->Grade->find('all'); 
        $grades = $this->Grade->getGrades();

        //Returns the $grades variable when it is requested. 
        if($this->request->is('requested')){
            return $grades;
        }

        //Sends the $grades variable to the view file. The string 'grades' is the name of the variable that the Grades View file will have the same setup as the $grades.
        $this->set('grades', $grades);
    }
}
4

1 回答 1

0

我想知道有没有办法任意选择一个键(第一个0-5)?

是的,php,不是 CakePHP

$keys = array_keys($grades);
$foo = $keys[rand(0, count($keys) - 1)]

现在,我认为这不是你需要的。我看到您正在使用单选按钮,您想在单选按钮中显示什么?

编辑

假设此数据来自名为“Grade”的模型,您将在您的 crontroller 中执行此操作

$grades = $this->Grade->find('list');
$this->set(compact('grades'));

那么,在你看来:

echo $this->Form->input('grade_selection', array('options' => $grades));
于 2013-11-12T01:20:11.283 回答