我正在尝试为Prestashop模块设置一个时区下拉列表。 我按照这个例子 - http://doc.prestashop.com/display/PS16/Using+the+HelperForm+class#UsingtheHelperFormclass-Selector
这是我用来获取时区列表的代码:
function timezones() {
$timezones = [];
foreach (timezone_identifiers_list() as $timezone) {
$datetime = new \DateTime('now', new DateTimeZone($timezone));
$timezones[] = [
'sort' => str_replace(':', '', $datetime->format('P')),
'offset' => $datetime->format('P'),
'name' => str_replace('_', ' ', implode(', ', explode('/', $timezone))),
'timezone' => $timezone,
];
}
usort($timezones, function($a, $b) {
return $a['sort'] - $b['sort'] ?: strcmp($a['name'], $b['name']);
});
return $timezones;
}
然后我尝试按照文档中的说明进行操作 -
$timezoneList = timezones();
$options = array();
foreach ($timezoneList as $timezone)
{
$options[] = array(
"id" => $timezone['offset'],
"name" => '(UTC '.$timezone['offset'].') '.$timezone['name'].''
);
}
我的结果产生了一个下拉列表,其中包含我想要的列表,但是值为空并且在下面抛出错误 - 注意文件F中的第786行:\ xampp \ htdocs \ presta02 \ vendor \ prestashop \ smarty \ sysplugins \ smarty_internal_templatebase.php(157):eval()' d code [8]未定义的索引:id_option
我的目标是得到这样的东西 -
<option value="-11:00">(UTC -11:00) Pacific, Midway</option>
目前我得到了这个 -
<option value="">(UTC -11:00) Pacific, Midway</option>
答案 0 :(得分:2)
如果您已从文档中使用此部分
array(
'type' => 'select', // This is a <select> tag.
'label' => $this->l('Shipping method:'), // The <label> for this <select> tag.
'desc' => $this->l('Choose a shipping method'), // A help text, displayed right next to the <select> tag.
'name' => 'shipping_method', // The content of the 'id' attribute of the <select> tag.
'required' => true, // If set to true, this option must be set.
'options' => array(
'query' => $options, // $options contains the data itself.
'id' => 'id_option', // The value of the 'id' key must be the same as the key for 'value' attribute of the <option> tag in each $options sub-array.
'name' => 'name' // The value of the 'name' key must be the same as the key for the text content of the <option> tag in each $options sub-array.
)
),
然后你必须在$ options数组中使用相同的键。这意味着你必须使用这个
$timezoneList = timezones();
$options = array();
foreach ($timezoneList as $timezone)
{
$options[] = array(
"id_option" => $timezone['offset'],
"name" => '(UTC '.$timezone['offset'].') '.$timezone['name'].''
);
}
或将您的选择定义选项键从id_option
变为id
'options' => array(
'query' => $options, // $options contains the data itself.
'id' => 'id', // The value of the 'id' key must be the same as the key for 'value' attribute of the <option> tag in each $options sub-array.
'name' => 'name' // The value of the 'name' key must be the same as the key for the text content of the <option> tag in each $options sub-array.
)
顺便说一句,这条评论也说明了这个
// The value of the 'id' key must be the same as the key for 'value' attribute of the <option> tag in each $options sub-array.