Undefined index: page in
Asked Answered
A

7

7

Ok I have the above error on my page, I am using PHP 5.3 for the first time, and have used this code before but never had this notice, so I was hoping for some guidance. I have looked at some questions but when I try to apply the solution to my bit of code it doesn't seem to work.

This code is a PHP pagination script from paper mashup and so if I can find the answer it may help anybody else who is also using this code and experiences the same problem.

the piece of code which is generating the notice is this -

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

From what I have read it was suggested to add isset and so changed the code to look like it does below, however i still get the same error.

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

Any advice would be appreciated.

Thanks

Stan

Annabellannabella answered 17/9, 2012 at 23:5 Comment(2)
You should post the error/notice you are receiving from PHP for us to have a better chance at solving your issue.Eyde
Hi Nelson thanks for the advice, this is the full Notice: Undefined index: page in C:\wamp\www\jobboard_plugin\jobs\jobboard.php on line 56Annabellannabella
D
6

The 'undefined index' error is basically telling you that you tried to get something from an array using an index for which there is no matching element.

In this case, the problem is in the line above your isset() call. The index 'page' is not defined in your $_GET array. So you need to first check if $_GET['page'] is set:

if (isset($_GET['page'])) {
  $page = mysql_real_escape_string($_GET['page']);
  // do more stuff
}
Doggoned answered 17/9, 2012 at 23:8 Comment(1)
Thank you that makes perfect sense,Annabellannabella
C
1

i got some issue.. this is the best and shortest solution i've got..

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

or

$page = (isset($_GET['page'])) ? $_GET['page'] : 0;
Copulate answered 12/8, 2015 at 18:44 Comment(0)
G
0

The error is on your first line do

$page = isset($_GET['page']) ? mysql_real_escape_string($_GET['page']) : 0;
Godard answered 17/9, 2012 at 23:8 Comment(2)
Worked Perfect, Thank you I will highlight as the answer in 4 mins.Annabellannabella
Hi, I got a bit a head of myself it looked great but the pagination didn't work, although it did remove the notice.Annabellannabella
B
0

In your code you check wether the page is not set, and then use the page, resulting in the same notice. Simply remote the exclamation mark. Try this:

$page = mysql_real_escape_string($_GET['page']);
 if(isset($page)){
    $start = ($page - 1) * $limit; 
}else{
    $start = 0; 
}
Blus answered 17/9, 2012 at 23:9 Comment(3)
Hi Thanks, I added your piece of code and then it kept the same notice but also returned the following: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\jobboard_plugin\jobs\jobboard.php on line 172Annabellannabella
Which relates to this piece of code: while($row = mysql_fetch_array($result))Annabellannabella
That sounds like a different piece of code. You could place an if around your while like: if ($result)Blus
H
0

Although I can't see it in your code, I would be willing to bet that you have an empty variable $_GET['page'] in your code. When you do a pagination, you need to keep appending the variable to the anchor tags if you are using the <a href="somePage.php?page=4 style of link.

Check your links to make sure that you keep passing it back all the time.

The error message undefined index appears when you try to access an array element that doesn't exist - and in this case, it is almost certainly the $_GET array.

Hawken answered 17/9, 2012 at 23:9 Comment(0)
G
0

Hope this will help

<?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>
Gumm answered 14/10, 2015 at 9:11 Comment(1)
You should explain in text what you have fixed, otherwise your answer may get deleted..Moxie
N
-1

You should do it like this

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

Undefined index: page refers to some array specific error. In your code the array with index value page is $_GET so it should be the first line of your code that needs some protection.

Neilla answered 17/9, 2012 at 23:10 Comment(3)
According to your code; if the $_GET['page'] is not set, neighter will the $start be. Probably causing trouble down the road.Blus
That is not the problem of the poster. He refers to the error of undefined index and here is the answer for that.. If $start is your worry then you probably want to worry about his whole project :)Neilla
That specific if else construction makes sure the $start will be set. Your code breaks that convention. If we really wanna make sure the notice won't show, we should advice him to place an exit or die() on top of his page ;)Blus

© 2022 - 2024 — McMap. All rights reserved.