gpt4 book ai didi

php - ZF2 : how to get options for dropdown from db

转载 作者:可可西里 更新时间:2023-11-01 00:48:34 26 4
gpt4 key购买 nike

我关注了 tutorial .但是我找不到像这样从数据库中选择填充表单的方法:

// Blog/src/Blog/Form/BlogItemForm.php
$blogCategoryTable = new Model\BlogCategoryTable;
$this->add(new Element\Select('category_id',
array('label' => 'Category', 'value_options' => $blogCategoryTable->getFormChoices())
));

有没有人有什么想法?

最佳答案

我使用一个函数来检索数据并将其设置为以下形式:

来 self 的工厂:

    $option_for_select = $this->model->getWhatEver();
$this->add($factory->createElement(array(
'name' => 'what_ever',
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'options' => $option_for_select,
),
'options' => array(
'label' => 'What ever:',
),
)));

来自模型:

public function getWhatEver()
{
$sql = "SELECT something";
$statement = $this->adapter->query($sql);
$res = $statement->execute();

// set the first option
$rows[0] = array (
'value' => '0',
'label' => 'Top',
'selected' => TRUE,
'disabled' => FALSE
);

foreach ($res as $row) {
$rows[$row['triplet_id']] = array (
'value' => $row['col1'],
'label' => $row['col2'],
);
}
return $rows;
}

关于php - ZF2 : how to get options for dropdown from db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12341114/

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