Page 1 of 1

undefined index.page

Posted: Tue Sep 16, 2014 9:47 pm
by kydude1957
When I test my code below i get a warning one every line that has

Code: Select all

if($_GET['page']
on it.

The warning is:

Undefined index: page with the page being index.php.

I am not on a server just using phpDesigner to test it. If I leave out the code and just use the includes the template portion loads fine.

Code: Select all

<?php


include("includes/header.html");
include("includes/navbar.html");
if($_GET['page'] == "resume"){
    
    include("includes/resume.html");
}

    if($_GET['page'] == "portfolio"){
        include("includes/portfolio.html");
    }


    if($_GET['page'] == "contact"){
       include("includes/contact.html"); 
    }
    
    if($_GET['page'] == "home"){
    
       include("includes/home.html");
}


include("includes/footer.html");

?>

nav bar:

Code: Select all

<div class='navbar'>
<div class='button'><a href='index.php'>Home</a></div>
<div class='button'><a href='index.php?page=resume'>Resume</a></div>
<div class='button'><a href='index.php?page=portfolio'>Portfolio</a></div>
<div class='button'><a href='index.php?page=contact'>Contact Me</a></div>

</div>

Re: undefined index.page

Posted: Tue Sep 16, 2014 10:43 pm
by Christopher
This problem is that $_GET['page'] is not defined. You could do something like this:

Code: Select all

<?php
include("includes/header.html");
include("includes/navbar.html");
if(isset($_GET['page'])) {
    if($_GET['page'] == "resume"){
        include("includes/resume.html");
    }

    if($_GET['page'] == "portfolio"){
        include("includes/portfolio.html");
    }

    if($_GET['page'] == "contact"){
       include("includes/contact.html"); 
    }
    
    if($_GET['page'] == "home"){
       include("includes/home.html");
    }
}
include("includes/footer.html");
Obviously there are cleaner ways to code the above. A switch() or checking if $_GET['page'] is in_array() would be better. But you really should look into the Front Controller pattern. Even better, use a framework with a Front Controller and Router.