如果可能的话,我想要一些帮助。
我创建了两个函数,以便在重定向后设置$_GET时显示一些消息。这是代码:
function display(){
if(isset($_GET['cnf_upd']) && $_GET['cnf_upd'] == '1'){
$value = "The update was successful!";
$type = "confirm";
construct_the_div($value, $type);
}
if(isset($_GET['err_upd']) && $_GET['err_upd'] == '1'){
$value = "The Update failed.";
$type = "error";
construct_the_div($value, $type);
}
if(isset($_GET['cnf_del']) && $_GET['cnf_del'] == '1'){
$value = "Deleted completely.";
$type = "confirm";
construct_the_div($value, $type);
}
if(isset($_GET['err_del']) && $_GET['err_del'] == '1'){
$value = "Unable to delete.";
$type = "error";
construct_the_div($value, $type);
}
}
function construct_the_div($value, $type){
// creating a div to display the message results
$div = "<div class='"{$type}Msg'">'n";
$div .= "<p>{$value}</p>'n";
$div .= "</div><!-- end of {$type}Msg -->'n";
echo $div;
}
我想做的是尝试改进显示功能,因为它越来越长,所以如果可能的话,可能只有一个(最多两个)if语句。因此,GET的值将在if条件内动态,并且如果它具有preffix"cnf_",则它将是"confirmMsg",如果它具有preffix"err_",那么它将是一个"errorMsg"。
有可能做这样的东西吗???
function display() {
$messages = array(
'cnf_upd' => 'The update was successful!',
'cnf_err' => 'The Update failed.!',
// ...
// add all error and confirm there
// ...
);
foreach($_GET as $key => $value) {
if(strpos($key, 'cnf_')===0) {
$type = 'confirm';
$value = isset($messages[$key])
? $messages[$key]
: $key;
construct_the_div($value, $type);
}
if(strpos($key, 'err_')===0) {
$type = 'error';
$value = isset($messages[$key])
? $messages[$key]
: $key;
construct_the_div($value, $type);
}
}
}
这种方法不正确,似乎一次只能出现一条消息(不能同时出现"完全删除"answers"无法删除")。尝试以这种方式构造参数:?msg=upd&msgType=cnf
function display(){
if (isset($_GET['msg']) && isset($_GET['msgType']))
{
$messages = array('cnf_upd'=>'The update was successful!',
'err_upd'=>'The update failed!',
'cnf_del'=>'The deletion was successful!',
'cnf_upd'=>'The deletion failed!',
);
if (isset($messages[$_GET['msgType'].'_'.$_GET['msg']))
construct_the_div($messages[$_GET['msgType'].'_'.$_GET['msg']], htmlspecialchars($_GET['msgType']));
}
还有很多需要改进的地方,但对于一开始来说,这更清洁、更安全。
我将提出一个不同的解决方案。不要根据要发送的消息在$_GET
中设置不同的参数,而是设置一个参数并解析其值。
// Start by setting integer constants:
define(CNF_UPD, 1);
define(ERR_UPD, 2);
define(CNF_DEL, 3);
define(ERR_DEL, 4);
然后,当您设置值un$_GET
时,使用常数:
// Build the URL with a deletion error...
header("Location: http://example.com/script.php?msg=" . ERR_DEL);
最后,使用switch
对它们进行解析
if (isset($_GET['msg'])) {
switch ($_GET['msg']) {
case CNF_UPD:
// Updated...
break;
case ERR_UPD:
// failed...
break;
// etc...
default:
// invalid code.
}
}
如果使用confirm/error/confirm/error
模式作为整数常量,则可以通过取$_GET['msg'] % 2
来确定它是哪一个。奇数是确认,偶数是错误。当然,你可以用很多其他的方式来表达,我只是碰巧按照你使用的交替顺序键入了它们。例如,您还可以对确认进行正整数运算,对错误进行负整数运算。
$type = $_GET['msg'] % 2 == 1 ? $confirm : $error;
这可以很容易地扩展为使用多条消息。由于它们是整数值,因此可以安全地构造逗号分隔的列表,并在收到它们时explode()
。
$messages = implode(array(ERR_DEL,CNF_UPD));
header("Location: http://example.com/script.php?msg=$messages");
除非你能以某种方式基于$_GET参数生成$value和$type(我看不出你会怎么做),否则你可以做这样的事情:
$messages = array();
$messages[] = array('id' => 'cnf_upd', 'value' => 'The update was successful!', 'type' => 'Confirm');
$messages[] = array('id' => 'err_upd', 'value' => 'The Update failed.', 'type' => 'error');
...
foreach ($messages as $message) {
if(isset($_GET[$message['id']]) && $_GET[$message['id']] == '1'){
construct_the_div($message['value'], $message['type']);
}
}