DarkNoise Posted April 26, 2007 Share Posted April 26, 2007 Hi everyone. I'm building a very large site and decided it would be best to start using PHP includes to build everything. however it got a bit complicated with the amount of page I had to turn into php's. So I built a template on one single page and put a PHP Include tag where I want the content. Now here's my problem. I have a DHTML menu, and I want the links to load up the different pages of content where the PHP Include tag is. Is there a special format for links to do this at all? To put it simply, I want each link to load a different page where the include tag is. Link to comment https://forums.phpfreaks.com/topic/48851-solved-php-include-help/ Share on other sites More sharing options...
Psycho Posted April 26, 2007 Share Posted April 26, 2007 Pass the include name on the query string. For example, let's say you have a contact.inc page, then have the link point to [/b]index.php?incl=contact[/b]. Then in the index page you could do this: if (isset($_GET['incl'])) { $includefile = $_GET['incl'] . '.inc'; } else { $includefile = 'home.inc'; } include ($includefile); You should also have some error handling in case someone puts a non-valid name on the query string. Link to comment https://forums.phpfreaks.com/topic/48851-solved-php-include-help/#findComment-239441 Share on other sites More sharing options...
DarkNoise Posted April 26, 2007 Author Share Posted April 26, 2007 So my links have to be index.php?incl=contact right. =contact is the name of the page I want to include? What if it's in a different folder? if (isset($_GET['incl'])) { $includefile = $_GET['incl'] . '.inc'; } else { $includefile = 'home.inc'; } include ($includefile); And I put this in replace of my include tag? Just like that or with <? at the start and ?> at the end as well? I take it the .inc is the file extention it's meant to look for and the home.inc is mean to be the default it first loads. Link to comment https://forums.phpfreaks.com/topic/48851-solved-php-include-help/#findComment-239451 Share on other sites More sharing options...
Psycho Posted April 27, 2007 Share Posted April 27, 2007 Well, I don't know how you have set up your directory structure, I just gave an example. Are all of your include files in the same directory? It would make it much easier. Then you could just append the directory path to the beginning of the variable name on the query string. If not, then I would suggest using a switch() <?php switch ($_GET['incl']) { case 'contact': $includefile = 'contact/contactus.php'; break; case 'about': $includefile = 'about/aboutus.php'; break; case '': default: $includefile = 'home/homepage.php'; break; } ?> And I put this in replace of my include tag? Just like that or with <? at the start and ?> at the end as well? Maybe yes, maybe no. I can't see your code. I would probably put the code to determine the include page at the top and just set the variable for the include page. Then use that variable in the include call. I take it the .inc is the file extention it's meant to look for and the home.inc is mean to be the default it first loads. Yes to both. Your files could just as well end in php, txt, mycustomextension, whatever. I typically standardize my naming conventions so that I can pull files/folders programatically without hard coding the names - as in my first example, unlike the 2nd example. Link to comment https://forums.phpfreaks.com/topic/48851-solved-php-include-help/#findComment-239495 Share on other sites More sharing options...
neel_basu Posted April 27, 2007 Share Posted April 27, 2007 Dont use .inc use .inc.php or .php Cause some hosts doesn't parces .inc as php scripts. And so its a big security Issue. Link to comment https://forums.phpfreaks.com/topic/48851-solved-php-include-help/#findComment-239606 Share on other sites More sharing options...
DarkNoise Posted April 27, 2007 Author Share Posted April 27, 2007 Thanks alot guys. You've really saved me there. I've used it like this as a test run <a href="jjj.php?incl=header">Intro</a></div> <a href="jjj.php?incl=footer">Intro</a></div> <? if (isset($_GET['incl'])) { $includefile = $_GET['incl'] . '.php'; } else { $includefile = 'footer.php'; } include ($includefile); ?> And it works a treat. Now... one question I'm still trying to get around is linking to content in other directories. This is because this is a very very very large website. would.. for example <a href="jjj.php?incl=/images/footer">Intro</a> work? Link to comment https://forums.phpfreaks.com/topic/48851-solved-php-include-help/#findComment-239663 Share on other sites More sharing options...
DarkNoise Posted April 27, 2007 Author Share Posted April 27, 2007 nevermind, turns out that does work. Ok, thanks everybody. You've help me out big time. Cheers! Link to comment https://forums.phpfreaks.com/topic/48851-solved-php-include-help/#findComment-239723 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.