2012-09-17 32 views
6

好的我在頁面上出現了上述錯誤,我第一次使用PHP 5.3,並且之前使用過此代碼,但從未收到此通知,所以我希望得到一些指導。我看過一些問題,但是當我嘗試將解決方案應用於我的代碼時,它似乎不起作用。未定義的索引:頁面

此代碼是紙質混搭的PHP分頁腳本,所以如果我能找到答案,它可以幫助其他人也使用此代碼並遇到同樣的問題。

那塊這是生成​​通知代碼是這樣的 -

$page = mysql_escape_string($_GET['page']); 
if($page){ 
    $start = ($page - 1) * $limit; 
}else{ 
    $start = 0; 
    } 

從我看過有人建議增加isset,因此改變了代碼看起來像下面這樣做,但我仍然得到同樣的錯誤。

$page = mysql_real_escape_string($_GET['page']); 
if(!isset($page)){ 
    $start = ($page - 1) * $limit; 
}else{ 
    $start = 0; 
    } 

任何意見,將不勝感激。

感謝

斯坦

+0

你應該張貼的錯誤/發現你從PHP正在接受的,我們有一個更好的機會解決你的問題。 – Nelson

+0

嗨尼爾森感謝您的建議,這是完整的通知:未定義的索引:頁面在C:\ wamp \ www \ jobboard_plugin \ jobs \ jobboard.php在線56 – user1678816

回答

3

的「未定義指數」的錯誤基本上是告訴你,你嘗試使用,以得到一個數組的東西沒有匹配元素的索引。

在這種情況下,問題出現在您撥打isset()的上方。您的$_GET陣列中未定義索引'page'。所以,你需要的,如果$_GET['page']設置爲先檢查:

if (isset($_GET['page'])) { 
    $page = mysql_real_escape_string($_GET['page']); 
    // do more stuff 
} 
+0

非常感謝, – user1678816

0

的錯誤是你的第一個行 做

$page = isset($_GET['page']) ? mysql_real_escape_string($_GET['page']) : 0; 
+0

工作完美,謝謝我將突出顯示爲4分鐘。 – user1678816

+0

嗨,我有點頭像我看起來不錯,但分頁沒有起作用,雖然它刪除了通知。 – user1678816

0

在你的代碼請檢查是否頁面沒有設置,然後使用頁面,導致同樣的通知。只需遙控感嘆號即可。試試這個:

$page = mysql_real_escape_string($_GET['page']); 
if(isset($page)){ 
    $start = ($page - 1) * $limit; 
}else{ 
    $start = 0; 
} 
+0

嗨,謝謝,我添加了一段代碼,然後它保持相同的通知,但也返回了以下內容:警告:mysql_fetch_array()期望參數1是資源,布爾在C:\ wamp中給出\ www \ jobboard_plugin \ jobs \ jobboard.php在線172 – user1678816

+0

這涉及到這段代碼:while($ row = mysql_fetch_array($ result)) – user1678816

+0

這聽起來像是一段不同的代碼。你可以放置一個如果你周圍,如:if($ result) –

0

雖然我不能看到它在你的代碼,我願意打賭,你在你的代碼中的空白變量$_GET['page']。當您執行分頁時,如果使用<a href="somePage.php?page=4樣式的鏈接,則需要將變量附加到錨標記。

檢查您的鏈接,以確保您保持始終將其傳回。

當您嘗試訪問不存在的數組元素時出現錯誤消息undefined index - 在這種情況下,它幾乎可以肯定是$_GET陣列。

-1

你應該做這樣的

if(isset($_GET['page']){ 
    $page = mysql_real_escape_string($_GET['page']); 
    if(!isset($page)){ 
    $start = ($page - 1) * $limit; 
    }else{ 
    $start = 0; 
    } 
} 

Undefined index: page是指某些特定array錯誤。在你的代碼中,indexpage的數組是$_GET,所以它應該是你的代碼的第一行,需要一些保護。

+0

根據你的代碼;如果沒有設置$ _GET ['page'],那麼$ start就會變得越來越近。可能會造成麻煩。 –

+0

這不是海報的問題。他指的是未定義索引的錯誤,這裏是答案。如果$ start是你的擔心,那麼你可能要擔心他的整個項目:) – Deepak

+0

這個具體如果其他結構確保$ start將被設置。你的代碼打破了這個約定。如果我們真的想確保通知不會顯示,我們應該建議他在他的頁面上放置出口或死亡();) –

0

我得到了一些問題..這是我得到的最好和最短的解決方案..

$page = (isset($_GET["page"]) ? $_GET["page"]:$config["per_page"] =''); 

$page = (isset($_GET['page'])) ? $_GET['page'] : 0; 
0

希望這將有助於

<?php 
# default page 
$default = 'home.php'; 

# set document root path 
$base = $_SERVER['DOCUMENT_ROOT'].'/redirect/'; 

# list of all site pages + the id they will be called by 
$pages = array('home' => 'home.php','about' => 'about.php','contact' => 'contact.php'); 
if (isset($_GET['page'])) { 
    $page = mysql_real_escape_string($_GET['page']); 
if(array_key_exists($_GET['page'], $pages)) 
{ 
foreach($pages as $pageid => $pagename) { 
if($_GET['page'] == $pageid && file_exists($base.$pagename)) 
{ 
      /* if somebody's making a request for ?page=xxx and 
      the page exists in the $pages array, we display it 
      checking first it also exists as a page on the server */ 
      include $base.$pagename; 
     } 
    } // end foreach 
} 
else { 
      /* if the page isn't listed in $pages, or there's no ?page=xxx request 
      we show the default page, again we'll also just make sure it exists as a file 
      on the server */ 
      if(file_exists($base.$default)) include $base.$default; 
} 
} else { 
    header ("Location: index.php?page=home"); 
} 


?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>My Website</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<body> 
<div style="height:50px;border:1px solid #000;margin-bottom:10px"> 

</div> 

<div style="float:left;width:15%;border:1px solid #000"> 
<ul style="margin-left:10px;list-style-type:none"> 
    <li><a href="index.php?page=home">Home</a></li> 
    <li><a href="index.php?page=about">About</a></li> 
    <li><a href="index.php?page=contact">Contact</a></li> 
</ul> 
</div> 

<div style="float:right;width:80%;border:1px solid #000"> 
    <div style="padding:4px"> 
    <!-- content here --> 
    </div> 
</div> 
</body> 
</html> 
+1

您應該在文本中解釋您已修復的問題,否則您的答案可能會被刪除。 。 –