2014-01-14 111 views
0

我有3個變量,分別命名爲$a,$b$c。這些變量保存來自數據庫的動態值。php中變量的比較

我想比較這些變量,如果$a$b$c保持空間的手段「」然後打印no message如果變量$a$b保持空間和$c保持信息打印消息,如果$a$c保持空間和$b保持信息打印消息,如果$b,$c保持空間並且$a保持消息打印消息。請幫幫我。由於

我的代碼是

if(($_GET['ms1']== " ") && ($_GET['ms2'] == " ") && ($_GET['ms3']== " ")) 

    { 
     print"<p style=color:red;text-align:center;font-family:Times New Roman,Georgia,Serif;>"; 
     echo "No Message From Court."; 
     print"</p>"; 
     print"<p style=color:green;text-align:center;font-family:Times New Roman,Georgia,Serif;>"; 
     echo "Thank You"; 
     print"</p>"; 

    } 

elseif(($_GET['ms1']==' ') && ($_GET['ms2']==' ')) 


     { 
      print"<p style=color:red;font-family:Times New Roman,Georgia,Serif;text-align:center;>"; 
      echo '3'.$_GET['ms3']; 
      print"</p>"; 


     } 

elseif(($_GET['ms1']== ' ') && ($_GET['ms3']==' ')) 


     { 
      print"<p style=color:red;font-family:Times New Roman,Georgia,Serif;text-align:center;>"; 
      echo '2'.$_GET['ms2']; 
      print"</p>"; 


     } 

    elseif(($_GET['ms2']==' ') && ($_GET['ms3']==' ')) 


     { 
      print"<p style=color:red;font-family:Times New Roman,Georgia,Serif;text-align:center;>"; 
      echo '1'.$_GET['ms1']; 
      print"</p>"; 


     } 
+1

問題是什麼?順便說一下,如果你想檢查一個單引號,你至少應該使用'==='來引入使用單引號作爲設置的問題。 – jeroen

+0

問題是,變量包含空間,所以我根本沒有比較,請幫助我先生 – user3181994

+0

我想,如果所有變量包含空格然後打印沒有消息,但在這三個,如果任何一個包含字符串消息,然後打印消息。 – user3181994

回答

0
<style> 
    .p_red { color:red;text-align:center;font-family:Times New Roman,Georgia,Serif; } 
    .p_green { color:green;text-align:center;font-family:Times New Roman,Georgia,Serif; } 
</style> 

<?php 
function isEmpty($string) { 
    return ($string === ' '); 
} 

$a = $_GET['ms1']; 
$b = $_GET['ms2']; 
$c = $_GET['ms3']; 

if (isEmpty($a) && isEmpty($b) && isEmpty($c)) { 
    echo "<p class='p_red'>No Message From Court</p>"; 
    print"<p class='p_green'>Thank You</p>"; 
} 

if (!isEmpty($a) && isEmpty($b) && isEmpty($c)) { 
    echo "<p class='p_red'>1. $a</p>"; 
} 

if (isEmpty($a) && !isEmpty($b) && isEmpty($c)) { 
    echo "<p class='p_red'>2. $b</p>"; 
} 

if (isEmpty($a) && isEmpty($b) && !isEmpty($c)) { 
    echo "<p class='p_red'>3. $c</p>"; 
} 
+0

謝謝你,先生好吧 – user3181994

0

這裏是一個非常簡單的實現:

<?php 
    foreach ($_GET as $key => $val) { 
    if (strpos($key, 'ms') { 
     if (!strpos($val, ' ')) { 
     echo '<p style="color:red;font-family:Times New Roman,Georgia,Serif;text-align:center;">'; 
     echo str_replace('ms', '', $key).$_GET[$key]; 
     echo '</p>'; 
     } 
    } 
    } 

希望這有助於

0

我會建議複製$ _GET項局部變量($ a,$ b,$ c)檢查那些條目(isset call)。如果任何/全部不是,請考慮將局部變量默認爲「(單個空格)」。那麼你可以安全地檢查if ($a == ' ')...等,知道你可以使用的值。

你應該考慮把常見的HTML代碼放到一個地方,而不是重複3次。

$a = (isset($_GET['ms1'])? $_GET['ms1']: ' '; 
$b = (isset($_GET['ms2'])? $_GET['ms2']: ' '; 
$c = (isset($_GET['ms3'])? $_GET['ms3']: ' '; 
echo '<p style="color:red;font-family:Times New Roman,Georgia,Serif;text-align:center;">'; 
if ($a == ' ' && $b == ' ' && $c == ' ') { 
    echo "No Message From Court."; 
    print"</p>"; 
    print"<p style=color:green;text-align:center;font-family:Times New Roman,Georgia,Serif;>"; 
    echo "Thank You"; 
} elseif ($a == ' ' && $b == ' ') { 
    echo '3'.$c; 
} elseif ($a == ' ' && $c == ' ') { 
    echo '2'.$b; 
} elseif ($b == ' ' && $c == ' ') { 
    echo '1'.$a; 
} 
echo '</p>'; 

請注意,您應該警惕注射討厭的HTML(iFrame等)時,簡單地呼應了用戶輸入(這是很容易彌補自己的GET字段)的用戶。

+0

謝謝你,先生,這對我很有幫助 – user3181994