PhalconPHP:如何在SELECT标记中设置Selected选项(PhalconPHP: How to set a Selected option in SELECT tag)

编程入门 行业动态 更新时间:2024-10-23 23:31:14
PhalconPHP:如何在SELECT标记中设置Selected选项(PhalconPHP: How to set a Selected option in SELECT tag)

我想用来自Db的值设置<option> 。 到目前为止,我试过这个但没有工作:

$site_select = new Select('site_id', $this->_sites);

I want to set the <option> with the value coming from Db. So far I tried this but not working:

$site_select = new Select('site_id', $this->_sites);

最满意答案

更新

正如Timothy建议的那样使用数据库列生成选择:

new Select('site_id', Sites::find(), array('using' => array('site_id', 'site_name')));

设置给定选择的选定值:

$site_select->setDefault('YOUR_DB_VALUE');

然而,关于Phalcon形式还有另一个可爱的伎俩。 您可以将数据库实体传递给表单类,您的表单将自动填充。

形成:

class YourFormClass extends Phalcon\Forms\Form { public function initialize($entity = null, $options = null) {

控制器:

$entity = YourModel::findFirst(); $form = new YourFormClass ($entity, $options);

请注意,表单输入名称必须与DB / Model列匹配。

Update

Generating the select using DB columns as Timothy recommended:

new Select('site_id', Sites::find(), array('using' => array('site_id', 'site_name')));

Setting the selected value of the given select:

$site_select->setDefault('YOUR_DB_VALUE');

However there is another lovely trick about Phalcon forms. You could pass your DB entity to the form class and your form will be auto populated.

Form:

class YourFormClass extends Phalcon\Forms\Form { public function initialize($entity = null, $options = null) {

Controller:

$entity = YourModel::findFirst(); $form = new YourFormClass ($entity, $options);

Note that Form input names must match DB/Model columns.

更多推荐

本文发布于:2023-08-05 06:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1430234.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标记   选项   如何在   SELECT   PhalconPHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!