2011-11-22 29 views
-1

如果我有一個名爲file.php的文件,並且想要在其中顯示3個不同的內容。你會經常file.php和其他2將file.php?c=1file.php?c=2頁面內的基本php代碼

我該怎麼做呢?

<html> 
<head> 
    ........... 

<body> 

something goes here 1 <show only if "link.com/file.php?c=1 or file.php" 

something goes here 2 <show only if "link.com/file.php?c=2 

something goes here 3 <show only if "link.com/file.php?c=3  



</body> 
</html> 
+2

'if($ _GET ['c'] == 1)...' – JJJ

+1

請澄清你的要求 –

回答

1

嘗試這種情況:

if(!isset($_GET[ 'c' ])){ 
    # perform file.php actions 
} else if($_GET[ 'c' ] == 1) { 
    # perform file.php?c=1 actions 
} else if($_GET[ 'c' ] == 2) { 
    # perform file.php?c=2 actions 
} 

第一個條件檢查是否存在用於paremeter c一個querystring。如果沒有,那麼你有你的file.php常規行動。第二和第三個將讓你知道你有一個參數c和執行任何操作設置如果值是1或2

沒有!isset(...檢查,你可能會得到這樣的錯誤:

Notice: Undefined index: c 
1
// $c defaults to 1, if &c=xxx is specified, $c will be intval($_GET['c'])  
$c = isset($_GET['c']) ? intval($_GET['c']) : 1; 

if ($c == 1) { 
    require('f1.php'); 
} else if ($c == 2) { 
    require('f2.php'); 
} ... 
0
if(!isset($_REQUEST['c'])){ 
    require_once(file.php); 
} 
else if(isset($_REQUEST['c'])==1){ 
    require_once(file1.php); 
} 
else if(isset($_REQUEST['c'])==2){ 
    require_once(file2.php); 
} 

//現在你可以根據你的要求和條件使用這個工作。