带有 watchdog_severity_levels() 的复选框
我有这段代码可以让我从看门狗严重性的一些复选框中取出:
I have this code that gets me out some checkboxes with the watchdog severities:
/**
* Checkbox for errors, alerts, e.t.c
*/
foreach (watchdog_severity_levels() as $severity => $description) {
$key = 'severity_errors' . $severity;
$form['severity_errors'][$key] = array(
'#type' => 'checkbox',
'#title' => t('@description', array('@description' => drupal_ucfirst($description))),
'#default_value' => variable_get($key, array()),
);
return system_settings_form($form);
}
我在代码中将此 $key 设置为:
I set this $key in my code as:
$key = array_filter(variable_get($key,array()));
我认为这个集合是错误的,因为 drupal 让我出错.在这组 $key 之后,我用下面的 foreach 语句调用它,有人可以帮我做这件事吗?
I think this set is wrong as the drupal gets me out error. After that set of $key I call it with the following foreach statement could someone help me with that thing?
foreach ($key as $value) {
if ($value == 'warning') {
blablblablabla....
}
elseif ($value == 'notice') {
blablablbalbal....
}
}
推荐答案
使用您的逻辑,您将存储以下键 severity_errors0
、severity_errors1
、severity_errors2
, ... 在 variable
表中,因为 foreach 的 $severity
键只是索引.
Using your logic, you would store following keys severity_errors0
, severity_errors1
, severity_errors2
, ... in the variable
table because the $severity
key of your foreach is just the index.
将一组选定的严重性级别作为一个条目存储在变量表中不是更容易吗?
这里有一些示例代码可以为您完成这项工作:
Here some example code which does the job for you:
// Retrieve store variable
$severity_levels = variable_get('severity_levels', array());
// Declare empty options array
$severity_options = array();
// Loop through each severity level and push to options array for form
foreach (watchdog_severity_levels() as $severity) {
$severity_options[$severity] = t('@description', array(
'@description' => drupal_ucfirst($severity),
));
}
// Generate checkbox list for given severity levels
$form['severity_levels'] = array(
'#type' => 'checkboxes',
'#options' => $severity_options,
'#default_value' => array_values($severity_levels),
);
return system_settings_form($form);
现在要检索选定的严重级别,您可以执行以下操作:
Now to retrieve the selected severity levels, you do something like this:
// Retrieve store variable
$severity_levels = variable_get('severity_levels', array());
foreach ($severity_levels as $level => $selected) {
if (!$selected) {
// Severity level is not selected
continue;
}
// Severity level is selected, do your logic here
// $level
}
相关文章