1

I want paging is a content of a news blog. Everything works correctly, the page content is successful. But I get an error screen displays PHP:

Notice: Undefined index: page in C:\wamp\www\index.php on line 146

code with the line that gives the error:

$maxreg = 1;

    $pag = $_GET['page'];

if (!isset($pag) || empty($pag)){

      $min = 0;
      $pag = 1;  

}else{

      if($pag == 1){

            $min = 0;

      }else{

            $min = $maxreg * $pag;
            $min = $min - $maxreg;

      }
}

include("js/class.AutoPagination.php");
$obj = new AutoPagination(contar_contenido(), $pag);

mostrar_contenido($min,$maxreg);

echo $obj->_paginateDetails();

The line gives the error is this:

$ page = $ _GET ['page'];

The first page by default index.php and contains no var in the url. I do not understand why if fails below by a conditional var I determine if that has content or not.

Should not show any error php, could someone give me a solution? Thanks

PlayerWet
  • 419
  • 5
  • 18
  • make sure `?page=123` is in your URL (in your browser URL where you type) – pavlovich Nov 28 '15 at 08:19
  • It works perfectly, I went to page three and takes me to page 3. On that page does not fail. The url: http://localhost/index.php?&page=3 Everything is perfect, only fails when you first open the page and there is no var on url. – PlayerWet Nov 28 '15 at 08:23

3 Answers3

2

You don't need to do

$pag = $_GET['page'];

Check if $_GET['page'] is set first:

if(!isset($_GET['page']) || empty($_GET['page'])) {

    $min = 0;
    $pag = 1;

}else{

    if($pag == 1){

        $min = 0;

    }else{

        $min = $maxreg * $pag;
        $min = $min - $maxreg;

    }
}
pavlovich
  • 1,935
  • 15
  • 20
1

I quote;

Declare your variables. Or use isset() to check if they are declared before referencing them;

PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"

Community
  • 1
  • 1
Dave LB
  • 26
  • 4
0

I found the solution, I've built so, assigning a value to the variable empty get in if it is indefinite. Maybe it's a very elegant solution, but it works perfect paging.

 if(!isset($_GET['page']) || empty($_GET['page'])) {

        $_GET['page']="";

        }


        $maxreg = 2;
        $min = 0;
        $pag = $_GET['page'];

I think the second conditional left over.

I leave it here in case anyone has the same problem. This way you can fix it.

PlayerWet
  • 419
  • 5
  • 18