Technaut Posted July 11, 2009 Share Posted July 11, 2009 okay this is hard for me but easy for you.. i have a main file index.php which has loads of code that I have written, I need the same code to be in loads of other files on the site. The code includes, headers, menus, left column, right column etc. Basically the only differrent part on each of the files will be some content. I was thinking that instead of having one big index.php file and having to copy and paste that code to loads of other files that I could make many small files such as header.php, footer.php, leftcol.php and so on. Then once in each file I could somehow reference leftcol.php and that would always show. The main reason I want to do this is if I change the code for the left column I'd rather just updated a leftcol.php file and this will flow through to all the other files instead of updating every file every time. as I said, easy for you but hard for me Quote Link to comment https://forums.phpfreaks.com/topic/165608-insert-results-from-other-php-files/ Share on other sites More sharing options...
.josh Posted July 11, 2009 Share Posted July 11, 2009 So...what's the question? Seems like you have a solid plan going. Are you asking how to include each piece? include or require Quote Link to comment https://forums.phpfreaks.com/topic/165608-insert-results-from-other-php-files/#findComment-873518 Share on other sites More sharing options...
cs.punk Posted July 11, 2009 Share Posted July 11, 2009 ... Put your different 'code' into $varibles... And then just dump them in one files, include this page to all your others and just echo "$example_head"; echo "$example_logo"; echo "$example_mainframe"; Quote Link to comment https://forums.phpfreaks.com/topic/165608-insert-results-from-other-php-files/#findComment-873523 Share on other sites More sharing options...
phorman Posted August 15, 2009 Share Posted August 15, 2009 PHP has built in functions to help you in that regard, specifically for that task. include, require, include_once, and require_once. The difference are that require will not allow your program to continue, unless the file actually exists. Include will try to include the file, but if it does not exist, it will skip it, and continue. The _once option means that it will include the file only once no matter how many times you call it from other libraries. So... You could say.. require("header.php"); require("left_side.php"); require("footer.php"); Also, the files you are including could end in .html , they do not have to be .php files, unless you have scripts that need to be processed inside them. Quote Link to comment https://forums.phpfreaks.com/topic/165608-insert-results-from-other-php-files/#findComment-899050 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.