未定义的索引:id_option Prestashop 选择助手中的错误
Undefined index: id_option Error in Prestashop Selection Helper
我正在尝试为 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:\xampp\htdocs\presta02\vendor\prestashop\smarty\sysplugins\smarty_internal_templatebase.php(157) 中第 786 行的通知:eval() 的代码
[8] 未定义索引:id_option
我的目标是得到这样的东西 -
<option value="-11:00">(UTC -11:00) Pacific, Midway</option>
目前我明白了 -
<option value="">(UTC -11:00) Pacific, Midway</option>
如果您使用了文档中的这一部分
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'].''
);
}
或将您的 select 的定义选项键从 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.
我正在尝试为 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:\xampp\htdocs\presta02\vendor\prestashop\smarty\sysplugins\smarty_internal_templatebase.php(157) 中第 786 行的通知:eval() 的代码 [8] 未定义索引:id_option
我的目标是得到这样的东西 -
<option value="-11:00">(UTC -11:00) Pacific, Midway</option>
目前我明白了 -
<option value="">(UTC -11:00) Pacific, Midway</option>
如果您使用了文档中的这一部分
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'].''
);
}
或将您的 select 的定义选项键从 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.