如何在yii2中生成下拉列表 [英] how to generating a dropdown list in yii2

查看:23
本文介绍了如何在yii2中生成下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用此链接在 yii2 中制作下拉列表:如何在yii2中制作下拉列表?

I tried to make a dropdown list in yii2 using this link : How to make a drop down list in yii2?

我的代码是:

<?php use yii\helpers\ArrayHelper;
use app\models\Product;
?>
<?= $listdata=ArrayHelper::map(Product::find()->all(),'id','name'); ?>
<?= $form->field($model, 'parent_id')-> dropDownList($listdata);  ?>

但是我在使用 ArrayHelper
时遇到了问题问题是:PHP 通知 – yii\base\ErrorException数组到字符串的转换.......!我测试了以下代码:

but I have a problem in line of using ArrayHelper
the problem is: PHP Notice – yii\base\ErrorException Array to string conversion.......! I tested the below code :

 $listData=ArrayHelper::map(Product::find()->asArray()->all(),'id','name');     

但它没有解决并出现相同的错误!

but it dos not solved and has the same error!

有什么问题吗?有人可以帮我吗?

whats the problem? can somebody help me?

推荐答案

您正在尝试回显数组,请将 <?= 更改为 <?php:

You are trying to echo an array, change <?= to <?php in:

<?= $listdata=ArrayHelper::map(Product::find()->all(),'id','name'); ?>

这篇关于如何在yii2中生成下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆