可能重复:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
Reference - What does this error mean in PHP?
我正在创建一个项目数据库,但随后出现错误:
注意:未定义的索引:p在/home/timmycph/public_html/ItemDatabase/index.php第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>
我该如何解决?
答案 0 :(得分:0)
如果数组p
中没有元素$_GET
,则会发生这种情况,因为尽管您在创建$page
以解决此问题时遇到了麻烦,但您还是去了无论如何,在任何地方使用$_GET['p']
。
答案 1 :(得分:0)
将第15行的条件更改为
if ((isset($_GET['p'])) && $_GET['p'] == "") {
答案 2 :(得分:0)
变化:
if($_GET['p'] == ""){$_GET['p'] = '0';}
到
if(!isset($_GET['p']) || empty($_GET['p'])){
$_GET['p'] = 0;
}