Php 无法在查看页面中调用下拉列表
我正在研究codeigniter我的助手代码是Php 无法在查看页面中调用下拉列表,php,codeigniter,Php,Codeigniter,我正在研究codeigniter我的助手代码是 function dropdown($data, $select) { $list = "<select name='country'>"; foreach ($data as $key => $value) { $key1 = $value['countrycode']; $value1 = $value['countryname']; if ($select =
function dropdown($data, $select) {
$list = "<select name='country'>";
foreach ($data as $key => $value) {
$key1 = $value['countrycode'];
$value1 = $value['countryname'];
if ($select == $key1)
$list .= "<option selected=\"selected\" value=\"$key1\">$value1</option>";
else
$list .= "<option value=\"$key1\">$value1</option>";
$key1 = '';
$value1 = '';
}
$list .= "</select>";
return $list;
}
模型是
function getCountry() {
$this->db->select("countrycode,countryname");
$query = $this->db->get('country');
return $query->result_array();
}
我的实际问题是,在调用dropdown时,由于错误消息:为foreach()提供的参数无效,我无法在view页面中调用它
我不知道两个如何处理它您必须从
allCountry
函数中获得结果,并将其传递给您的下拉列表
生成器:
<?php
function allCountry() {
$result['data'] = $this->Country_mod->getCountry();
$data['country_list'] = $result['data'];
return $data; //must return the data
}
...
$countryData = $this->Country_mod->allCountry();
...
$list = dropdown($countryData, $someCountryCode);
请试试这个
在控制文件中
$this->load->view("view",$data);
视图文件
...
dropdown($country_list);
...
您的
下拉列表()
函数需要$data
中的数组。在何处/如何调用下拉列表
并向其传递数据?或者您希望allCountry()
实现该函数本地范围内的$data
数组,而dropdown()
函数对此一无所知?首先,如何解决它,您可以回答我的问题,因为您不清楚您是如何引用$data
值的。我可以将它作为数组传递给$country\u list,您调用dropdown()
函数,调用时是否将数组传递到$data
变量?
...
dropdown($country_list);
...