0

I have a bit of a problem in my PHP programming so I'll get to the deal.

This is the code that's bugging me:

$pageNum =  $_GET['page'];
      if(!isset($pageNum)) $pageNum=1;

Now, the idea is that instead you have 3 items on your page, trough PHP awesomeness, you have only 2 (from 3), 3rd being on another page, just as you have those page numbers on the bottom.

It constantly says:

Notice: Undefined index: page in C:\xampp2\htdocs\autosalon_db\model Offer.php on line 3

(that one php file)

and the other one says this also:

Notice: Undefined index: stranica in C:\xampp2\htdocs\autosalon_db\index.php on line 10

(another file)

Now anyone can explain to me where I'm going wrong?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • 1
    Please check `$_GET['page']` within the isset. You can use something such as `$pageNum = **default**; if (!isset($_GET['page'])) $pageNum = $_GET['page'];`. – Dave Chen Nov 29 '13 at 03:07
  • 1
    @DaveChen Or even more simply `$pageNum = !empty($_GET['page']) ? (int) $_GET['page'] : 1;` – Phil Nov 29 '13 at 03:34

1 Answers1

0

You are not providing the full code—or the URLs that trigger the code—so I will recode a snippet based on what you have provided. The problem here is you are assigning a variable based on $_GET before checking whether it exists. And you are checking if that variable is set afterwards.

$pageNum =  $_GET['page'];
      if(!isset($pageNum)) $pageNum=1;

Try this instead.

$pageNum = 1;
if (array_key_exists('page', $_GET) && !empty(trim($_GET['page']))) {
  $pageNum =  $_GET['page'];
}

What I am doing is setting a default for $pageNum before anything happens. Then the if checks if the key page even exists in $_GET and if that value is empty or not. If it’s not empty, it will set $pageNum to be the value of $_GET['page'].

Should help a bit. But again, your question is missing some data needed for real assessment.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103