PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
kydude1957
Forum Commoner
Posts: 31 Joined: Sun Sep 14, 2014 1:37 am
Post
by kydude1957 » Tue Sep 16, 2014 9:47 pm
When I test my code below i get a warning one every line that has
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>
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Tue Sep 16, 2014 10:43 pm
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.
(#10850)