-2

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
Reference - What does this error mean in PHP?

I was making an Item Database, but then an error showed:

Notice: Undefined index: p in /home/timmycph/public_html/ItemDatabase/index.php on line 15 http://timmycp.host.org/ItemDatabase/

The coding is:

  <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>

How can I fix that?

Community
  • 1
  • 1

3 Answers3

0

That'll occur when there is no element p in the array $_GET, because despite the fact that you went to the trouble of creating $page to cover this case, you go on to use $_GET['p'] everywhere instead anyway.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

change your condition on line 15 to

if ((isset($_GET['p'])) && $_GET['p'] == "") {
Pankaj Khairnar
  • 3,028
  • 3
  • 25
  • 34
0

change:

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

to

if(!isset($_GET['p']) || empty($_GET['p'])){
    $_GET['p'] = 0;
}
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62