2013-04-15 105 views
0

我試圖使用GET url中的查詢來確定頁面的內容。 這是我(編輯爲清楚起見句子):比較PHP中的URL字符串

<?php 
//decalre variables 
$title =''; 
$welcome = ''; 
$params = ''; 

$params = $_SERVER['QUERY_STRING']; 
echo $_SERVER['QUERY_STRING']; 
echo $params; 

if ($params='') { 
    header('start.html') ; 
} else { 
    if ($params === "selection=republic") { 
     //echo $params; 
     //echo 'Republic'; 
     $title = "Private"; 
     $welcome = "Our ."; 
     $signoff = "The Republic will strike back!"; 
    } 
    else if ($params === "selection=rebels") { 
     //echo $params; 
     //echo 'Rebels'; 
     $title = "Comrade"; 
     $welcome = "Hey comrade, welcome to the Underground Network!"; 
     $declaration="You see,o's!"; 
     $signoff = "Rebel!!"; 
    } 
    else if ($params === "selection=robots"){ 
     //echo $params; 
     //echo 'Robots'; 
     $title = "Bit"; 
     $welcome = "Our data "; 
     $declaration="Knowledge w."; 
     $signoff = "ed now."; 
    } 
    else { 
     echo 'There was an error - please go back.'; 
    } 
} 

第一回波顯示正確的URL,但比較卡在第三個選項。 幫助!

+5

我相信第一個'如果($ PARAMS = '')'應該是'=='。 – wroniasty

+1

爲什麼不使用'$ _GET ['selection']'? –

+0

if((空($變量))而不是if($ variable =='')...用於檢測是否沒有值或空白((和0也是如此注意整數))更好。 – aleation

回答

1

解析查詢字符串比$SERVER['QUERY_STRING']有更好的方法,具體來說,您可以使用$_GET來訪問特定的參數。例如:www.example.com?name=Dave & age = 30 ... 要取得名稱,可以做$_GET['name'],它會返回Dave。我認爲更好的方式來做到這一點會是這樣的:

$selection = $_GET['selection']; 
if (empty($selection)) { 
    header('start.html') ; 
} 

else { 
    $vars = array(
      'republic'=>array('title'=>'Private', 'welcome'=> 'Our .', 'declaration'=>'', 'signoff' => 'The Replublic will strike back'), 
      'rebels'=>array('title'=>'Comrade', 'welcome' => "Hey comrade, welcome to the Underground Network!", 'declaration'=>"You see,o's!",'signoff' => "Rebel!!"), 
      'robots'=>array('title'=>'Bit', 'welcome'=>'Our data', 'declaration'=>'Knowlegge W', 'signoff'=>'Ed now') 
    ); 

     list($title, $welcome, $declaration, $signoff) = $vars[$selection]; 
} 
+0

請更正錯誤和格式:) – mkjasinski

+0

那裏亞去,格式化和固定 – dave

+0

和'$ SERVER ['QUERY_STRING']'? – mkjasinski

1

這來自於三重=標誌,用於比較值和類型。你應該看到the difference here

我建議你只使用兩個平等的,順便說一下,你可以通過使用$ _GET [「選擇」]變量,而不是減輕你的代碼:

<?php 
//decalre variables 
$title =''; 
$welcome = ''; 
$params = ''; 

$params = $_SERVER['QUERY_STRING']; 
echo $_SERVER['QUERY_STRING']; 
echo $params; 

if (!isset($_GET['selection']) { // Check whether selection is set 
    header('start.html') ; 
} else { 
    if ($_GET['selection'] == "republic") { 
     //echo $params; 
     //echo 'Republic'; 
     $title = "Private"; 
     $welcome = "Our ."; 
     $signoff = "The Republic will strike back!"; 
    } 
    else if ($_GET['selection'] == "rebels") { 
     //echo $params; 
     //echo 'Rebels'; 
     $title = "Comrade"; 
     $welcome = "Hey comrade, welcome to the Underground Network!"; 
     $declaration="You see,o's!"; 
     $signoff = "Rebel!!"; 
    } 
    else if ($_GET['selection'] == "robots"){ 
     //echo $params; 
     //echo 'Robots'; 
     $title = "Bit"; 
     $welcome = "Our data "; 
     $declaration="Knowledge w."; 
     $signoff = "ed now."; 
    } 
    else { 
     echo 'There was an error - please go back.'; 
    } 
}