2013-02-04 22 views
2

如果可能,我想請一些幫助。PHP提高我的顯示信息功能

我已經爲了顯示某些郵件中創建兩個函數時,一個redirect.Here的代碼後設置一個$ _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條件下動態地變化,如果它有prefix'cnf_'則它將成爲'confirmMsg',並且如果它有preffix'err_',它將成爲'errorMsg'。

是否有可能使這樣的事情?

+0

我不不喜歡在獲取參數時顯示消息的方法。如果您在網址中輸入它,則即使沒有任何操作,也會顯示該消息。將會話保存在會話中並顯示,如果有的話會更好。因此,您還可以保存每個操作的自定義錯誤消息。 – bitWorking

回答

1
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); 
     } 

    } 
} 
+0

謝謝!我已經測試過它並且工作得很好!我認爲這種方式更加靈活,所以如果需要的話可以添加更多的消息 – Lykos

0

我打算提出一個不同的解決方案。根據要發送的消息而不是在$_GET中設置不同的參數,請設置一個參數並解析其值。

// Start by setting integer constants: 
define(CNF_UPD, 1); 
define(ERR_UPD, 2); 
define(CNF_DEL, 3); 
define(ERR_DEL, 4); 

然後,當你設置的值未$_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來確定它是哪一個。奇數是確認,evens是錯誤。當然,還有其他很多方法可以解決這個問題,我只是恰好按照您使用的交替順序輸入了它們。例如,您也可以爲正確的整數進行確認和否定錯誤。

$type = $_GET['msg'] % 2 == 1 ? $confirm : $error; 

這很容易擴展爲使用多個消息。由於它們是整數值,因此您可以安全地構造一個逗號分隔列表,並在收到它們時將它們組合起來。

$messages = implode(array(ERR_DEL,CNF_UPD)); 
header("Location: http://example.com/script.php?msg=$messages"); 
0

的做法是不正確的,似乎應該只出現在消息後(這裏不能「完全刪除」和「無法刪除」在一次)。 try結構的參數是這樣的:?味精= 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'])); 
} 

還有很大的改善,但對於啓動,這是更清潔,更安全。

0

除非你能以某種方式生成$ value並且基於$ _GET參數(我看不出你會怎麼做)$的類型,你可以這樣做:

$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']); 
    } 
}