-2

可能重复:
PHP:“注意:未定义的变量”和“注意:未定义的索引”</a>
参考 - 这个错误在 PHP 中是什么意思?

我正在创建一个项目数据库,但随后显示错误:

注意:未定义的索引:p in /home/timmycph/public_html/ItemDatabase/index.php on line 15 http://timmycp.host.org/ItemDatabase/

编码是:

  <html>
<head>
<body style="margin: 0 auto; text-align: center; padding: 50px; background: url(http://timmycp.co.cc/ItemDatabase/BG11.png) center top fixed no-repeat #1DABD8;">
<title>Club Penguin Item Database</title>
<link rel="stylesheet" href="style.css" type="text/css"/>
<center>
<body><center>
<table cellspacing="10">
<?php
$arr = json_decode(file_get_contents("http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json"),true);
$page = isset($_GET['p']) ? intval($_GET['p']) : 0;
$elementsPerPage = 10;
$elements = array_slice($arr, $page * $elementsPerPage, $elementsPerPage);
$totalpages = intval(count($arr)/$elementsPerPage);
if($_GET['p'] == ""){$_GET['p'] = '0';}
$whatpage = $_GET['p'];
if($whatpage > $totalpages){echo("<tr><td>Sorry this page does not exist. Please return to <a href='?p=0'>page 1</a>.</td></tr>"); exit;}
$link1 = $whatpage-1;
$link2 = $whatpage+1;
if($_GET['p'] == 0){$backbutton = "";} else {$backbutton = "<a href=?p=" . $link1 . ">Previous Page</a>";}
if($_GET['p'] == $totalpages){$nextbutton = "";} else {$nextbutton = "<a href=?p=" . $link2 . ">Next Page</a>";}
if($_GET['p'] == $totalpages or $_GET['p'] == 0){$middle = "";} else {$middle = " | ";}
$pagenav = $backbutton . $middle . $nextbutton;
echo('<tr><td colspan="7" align="center">'.$pagenav.'</td></tr>');
foreach($elements as $item)
{

$label = $item['label'];
$cost = $item['cost'];
$id = $item['paper_item_id'];
$member = $item['is_member'];
$type = $item ['type'];

if ($member == "1") {
$member = "Yes";
}else{
$member = "No";
}

if ($type == "1") {
$type = "Color";
}

if ($type == "7") {
$type = "Foot";
}

if ($type == "3") {
$type = "Face";
}

if ($type == "4") {
$type = "Neck";
}

if ($type == "5") {
$type = "Body";
}

if ($type == "6") {
$type = "Hand";
}

if ($type == "9") {
$type = "Background";
}

$str = '';
$str .= "<tr><td><embed src=\"http://www.timmycp.host.org/SWFViewer/items.swf?id=".$id."\"height='50' width='50' wmode='transparent'></td><td style='text-align: center !important;'><b>Name:</b> $label</td><td><b>Item ID:</b> $id</td><td><b>Members:</b> $member</td><td><b>Cost:</b> $cost coins</td><td><b>Type:</b> $type</td></tr>";

echo substr($str, 0, -5);
}
?></table></center></body></html>

我该如何解决?

4

3 回答 3

0

将第 15 行的条件更改为

if ((isset($_GET['p'])) && $_GET['p'] == "") {
于 2012-12-27T19:07:07.880 回答
0

改变:

if($_GET['p'] == ""){$_GET['p'] = '0';}

if(!isset($_GET['p']) || empty($_GET['p'])){
    $_GET['p'] = 0;
}
于 2012-12-27T19:07:18.023 回答
0

p当数组中没有元素时会发生这种情况$_GET,因为尽管您费力地创建$page以涵盖这种情况,但无论如何您仍然继续使用$_GET['p']任何地方。

于 2012-12-27T19:05:28.060 回答