-2

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

Here is my "index.php" page:

<?php

if($_GET['p']==4)
$p='dodaj.php';
if($_GET['p']==3)
$p='dodaj_wplate.php';
else if($_GET['p']==2)
$p='dodaj_u.php';
else
$p='index.php';
?>

Wybierz coś!.<br>
<a href="index.php?p=4">Dodaj osobę</a><br>
<a href="index.php?p=3">Dodaj wpłatę</a><br>
<a href="index.php?p=2">lDodaj ubezpieczenie</a><br>

<?

include $p;

?>

And I am getting error:

Notice: Undefined index: p in C:\xampp\htdocs\proj\index.php on line 3

Notice: Undefined index: p in C:\xampp\htdocs\proj\index.php on line 5

Notice: Undefined index: p in C:\xampp\htdocs\proj\index.php on line 7
Wybierz co!.
Dodaj osobę
Dodaj wpłatę
lDodaj ubezpieczenie

How can I define my "p"? Because I can see there is problem.

Also, not related to question, can you give me any link to layout tutorial related to php?

Community
  • 1
  • 1
trinny
  • 222
  • 1
  • 5
  • 22
  • 2
    `$page = isset($_GET['p']) ? $_GET['p'] : 'some_default_value';`, and use `$page` later on. Also, a `switch` may be better then a lot of `else if`s. – Wrikken Dec 13 '12 at 20:29
  • Next to what @Wrikken (rightly) wrote, you probably just only want to have an array that is mapping a number to a file. – hakre Dec 13 '12 at 20:32

2 Answers2

1

Try something like :

Wybierz cos!.<br>
<a href="<?php echo((isset($_GET['p']) && $_GET['p']==4) ? 'dodaj.php' : 'index.php?p=4');?>">Dodaj osobe</a><br>
<a href="<?php echo((isset($_GET['p']) && $_GET['p']==3) ? 'dodaj_wplate.php' : 'index.php?p=3');?>">Dodaj wplate</a><br>
<a href="<?php echo((isset($_GET['p']) && $_GET['p']==2) ? 'dodaj_u.php' : 'index.php?p=2');?>">lDodaj ubezpieczenie</a>
berty
  • 2,178
  • 11
  • 19
0
$p = 'index.php';
if( isset($_GET['p']) ){

    switch( $_GET['p'] ){

        case '2':
            $p = 'dodaj_u.php';
        break;

        case '3':
            $p = 'dodaj_wplate.php';
        break;

        case '4':
            $p = 'dodaj.php';
        break;

    }

}

?>

Wybierz coś!.<br>
<a href="index.php?p=4">Dodaj osobę</a><br>
<a href="index.php?p=3">Dodaj wpłatę</a><br>
<a href="index.php?p=2">lDodaj ubezpieczenie</a><br>

<?

include $p;

?>
Goran Usljebrka
  • 853
  • 7
  • 11