acornrevolution Posted May 4, 2007 Share Posted May 4, 2007 Hi everyone! I'm just starting to use PHP and am trying to figure out a simple (i think) code that uses the include() function. I want the index.php to automatically include the main content page, and then each link on my menu loads in the space where the main content page loaded. I had something like this, which didn't work: <?php if ($page && file_exists("$page.htm")) { @ require_once ("$page.htm"); } else { $page = "misc/intro"; @ require_once ("$page.htm"); } ?> Basically I want it to call up content.php automatically, and when a menu link is clicked on, it loads in that same space without changing index.php. How can I do this? Acorn! Quote Link to comment https://forums.phpfreaks.com/topic/49926-php-include-simple-code/ Share on other sites More sharing options...
warewolfe Posted May 4, 2007 Share Posted May 4, 2007 What error message are you getting? Quote Link to comment https://forums.phpfreaks.com/topic/49926-php-include-simple-code/#findComment-245019 Share on other sites More sharing options...
acornrevolution Posted May 4, 2007 Author Share Posted May 4, 2007 It just isn't working. Just a blank page. Quote Link to comment https://forums.phpfreaks.com/topic/49926-php-include-simple-code/#findComment-245021 Share on other sites More sharing options...
techtheatre Posted May 4, 2007 Share Posted May 4, 2007 just call index.php?page=content This should work...but you may have to pull this value from the "GET" variable (if your server is not set to do this automatically... make your first line (within teh <?php section of course) the following: $page = $_GET['page']; so your whole script is: <?php $page = $_GET['page']; if ($page && file_exists("$page.htm")) { @ require_once ("$page.htm"); } else { $page = "misc/intro"; @ require_once ("$page.htm"); } ?> your links will look like this: http://www.yoursitehere.com/index.php?page=welcome http://www.yoursitehere.com/index.php?page=about http://www.yoursitehere.com/index.php?page=contact etc... Quote Link to comment https://forums.phpfreaks.com/topic/49926-php-include-simple-code/#findComment-245022 Share on other sites More sharing options...
techtheatre Posted May 4, 2007 Share Posted May 4, 2007 one other modification...i always prefer to set up teh COMPLETE filename in a variable ahead of time, rahter than on the fly...this gives me more control, and also allows me to display it for troubleshooting...try this if you are still having trouble...and once it works, comment out the line that begins with "echo" by placing two slash marks before it (//) <?php $page = $_GET['page']; $filename = "$page.htm"; echo $filename; if ($page && file_exists("$filename")) { @ require_once ("$filename"); } else { $filename = "misc/intro.htm"; @ require_once ("$filename"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/49926-php-include-simple-code/#findComment-245025 Share on other sites More sharing options...
warewolfe Posted May 4, 2007 Share Posted May 4, 2007 Friendly advice. change the requires to includes and remove the error suppression symbol @ from the start of the lines while you are writing your script so you can see what is going wrong. Also, if you are using apache (I don't know how IIS or other servers do it) then go to the apache logs and check out the last few entries in the error log. Quote Link to comment https://forums.phpfreaks.com/topic/49926-php-include-simple-code/#findComment-245026 Share on other sites More sharing options...
acornrevolution Posted May 4, 2007 Author Share Posted May 4, 2007 This is what I used: <a href="index.php?page=green">Green</a><br> <a href="index.php?page=black">Black</a><br> <a href="index.php?page=red.htm">Red</a> <?php $page = $_GET['page']; if ($page && file_exists("$page.htm")) { @ require_once ("$page.htm"); } else { $page = "white.php"; @ require_once ("$page.htm"); } ?> I used colors to help see the changes better. It seems to be working, loading different pages in the same space, like I wanted, but only the text of the linked-to page comes up, and not a different bg color. Also, if I wanted the title to change with each page/link, is that possible? Quote Link to comment https://forums.phpfreaks.com/topic/49926-php-include-simple-code/#findComment-245028 Share on other sites More sharing options...
acornrevolution Posted May 4, 2007 Author Share Posted May 4, 2007 by the way, i never expected such fast replies and I am very grateful for them. I've been using html and css for a while, but I'm pretty new to PHP so all this help is great! THANKS AGAIN! Quote Link to comment https://forums.phpfreaks.com/topic/49926-php-include-simple-code/#findComment-245029 Share on other sites More sharing options...
trq Posted May 4, 2007 Share Posted May 4, 2007 but only the text of the linked-to page comes up, and not a different bg color. Also, if I wanted the title to change with each page/link, is that possible? Can we see one of these included pages? Quote Link to comment https://forums.phpfreaks.com/topic/49926-php-include-simple-code/#findComment-245032 Share on other sites More sharing options...
acornrevolution Posted May 4, 2007 Author Share Posted May 4, 2007 http://www.sunworkswebdesign.com/php Quote Link to comment https://forums.phpfreaks.com/topic/49926-php-include-simple-code/#findComment-245036 Share on other sites More sharing options...
techtheatre Posted May 4, 2007 Share Posted May 4, 2007 if you view the source of one of your "generated" pages you will see that you have the header twice. If you truly only want the content of the second page...then your original index.php file should not contain any output...just the PHP include() code. basically, wherever the include statement is, you will insert the ENTIRE code of the page you are including...so if headers or other info is already set, you end up with extra info and browsers get confused. If you are looking to include the WHOLE page though...then why not simply navigate to the page directly?...if you are trying to simply have the menu (or other common elements) appear on every page...include those instead of the rest of the page...so (example)...your "contact us" page might look like this (obviously fill in stuff where the ... is): <html> <head.... <body> include("page_top.php"); include("menu.php"); <p> Please contact us at:<br /> 123-456-7890<br /> Or Mail:<br /> 123 Nowhere Lane </p> include("footer.php"); </body> .... then your file (example: menu.php) might only contain the following (no header/body tags needed, as it is not ever displayed directly) <p> <a href="http://www.yoursitehere.com/welcome.php">welcome</a><br /> <a href="http://www.yoursitehere.com/about.php">about</a><br /> <a href="http://www.yoursitehere.com/contact.php">contact</a><br /> </p> Quote Link to comment https://forums.phpfreaks.com/topic/49926-php-include-simple-code/#findComment-245463 Share on other sites More sharing options...
acornrevolution Posted May 9, 2007 Author Share Posted May 9, 2007 All right! It works. I'm using the following code: <?php $page = $_GET['page']; if ($page && file_exists("$page.htm")) { @ require_once ("$page.htm"); } else { $page = "content/home"; @ require_once ("$page.htm"); } ?> If the page is missing, how do I get it to go to an error page or at least display an error message? Also, if I want the title to change with the links, I know there is some php title command. How do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/49926-php-include-simple-code/#findComment-249412 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.