2012-08-14 84 views
1

我想讓我的索引頁面顯示當前的季度和年份,以便隨着時間的推移而更新。我需要幫助重建我的代碼。這裏是。它像某種形式的公告牌日曆:如何在PHP中包含URL?

$now = new DateTime(); 
    $month = (int)$now->format("m"); 
      $get_year = date("Y"); 

    if ($month >= 1 AND $month <= 3) { 
     include_once("../index.php?year=".$get_year."&quarter=Q1"); 
    } 
    elseif ($month >= 4 AND $month <= 6) { 
     include_once("../jet/index.php?year=".$get_year."&quarter=Q2"); 
    } 
    elseif ($month >= 7 AND $month <= 9) { 
      include_once("../jet/index.php?year=".$get_year."&quarter=Q3"); 
    } 
    else { 
     include_once("../jet/index.php?year=".$get_year."&quarter=Q4"); 
    } 

將被顯示在頁面準備好了,它只是我不能它和結果顯示這些錯誤:

警告:include_once(... D:\ xampp \ htdocs \ jet \ index.php 121行上的結果太大

警告:include_once()[function.include]:無法在D:中包含(include_path ='.; D:\ xampp \ php \ PEAR')打開'.../index.php?year = 2012 & quarter = Q3' \ XAMPP \^h tdocs \ jet \ index.php上線121

幫助任何人?

+4

可能重複http://stackoverflow.com/questions/8301552/php-include-file-not-working-when-variables-are-put-in-url – fdomig 2012-08-14 06:34:07

+0

通過這篇文章http://stackoverflow.com/questions/5509929/passing-parameter-while-including-php-script – WatsMyName 2012-08-14 06:36:11

+0

1.你有什麼在121行index.php? – 2012-08-14 06:37:18

回答

3

區別。

讓我們回到基礎知識吧?

您通過URL發送的信息在另一端作爲「GET」接收,它需要以HYPERTEXT的形式發送到網絡服務器,該服務器會將信息傳遞給PHP腳本,並將相應地編譯它們。所以,這個邏輯將不起作用,因爲在包含你玩FILE SYSTEM的時候。

你想要做的是用頭()的

header("location: http://example.com/jet/index.php?year=$get_year&quarter=Q2"); 

代替

include_once("../index.php?year=".$get_year."&quarter=Q1"); 

header()將用戶重定向作爲HTTP響應。

+0

謝謝!這工作。我剛剛意識到我是如何忘記標題功能的。再次感謝! – jet 2012-08-14 07:08:41

+0

這不包括腳本,這將重定向服務器到另一個腳本。 – 2012-08-14 07:14:58

+0

@MinaYoussef這就是Jet將要實現的目標:) – Karma 2012-08-14 08:08:34

0

不要在包含字符串中傳遞$ _GET變量。

準備好

$year='2012'; 
$quarter='Q3'; 
include_once('index.php'); 

然後運行包含字符串變量,您可以訪問年度和季度正常。確保檢查變量的範圍。

所以,你的全碼:

$year=$get_year; 
if ($month >= 1 AND $month <= 3) { 
    $quarter='Q1'; 
    include_once("../index.php"); 
} 
elseif ($month >= 4 AND $month <= 6) { 
    $quarter='Q2'; 
    include_once("../jet/index.php"); 
} 
elseif ($month >= 7 AND $month <= 9) { 
    $quarter='Q3'; 
    include_once("../jet/index.php"); 
} 
else { 
    $quarter='Q4'; 
    include_once("../jet/index.php"); 
} 
+0

我不認爲這是他想要的;他在index.php中,他怎麼能再次包含index.php?這將是一個無限循環 – 2012-08-14 06:48:36

+0

他沒有提及哪個腳本包含哪個腳本。但他已經包含了index.php,從錯誤中可以看出。警告:include_once(.../index.php?year = 2012&quarter = Q3) – 2012-08-14 06:54:12

+0

我想在日期更改年份和月份時動態更改索引頁。你的答案看起來像索引將是靜態的,因爲變量是定義的,而不是基於日期函數。 – jet 2012-08-14 07:05:29