Including part of a file, not the whole file
Moderator: General Moderators
Including part of a file, not the whole file
I've learned to include a file into another with PHP Include. Now I want to include only part of a file e.g. refering to and including just one text block/HTML element among several stored in a single file. How can this be done with PHP Include (or suitable function)?
Please make it easy if possible, I'm just learning. God bless you.
Please make it easy if possible, I'm just learning. God bless you.
Re: Including part of a file, not the whole file
I don't believe you can easily include part of a file. An idea you could do would be to chop the html file up into smaller files and including them into that file and then whatever part you need, include just that file.
Re: Including part of a file, not the whole file
Likewatson516 wrote:I don't believe you can easily include part of a file. An idea you could do would be to chop the html file up into smaller files and including them into that file and then whatever part you need, include just that file.
Code: Select all
<?php include("header.php"); ?>
<html>
<body>
<?php include("body.php"); ?>
<?php include("footer.php"); ?>
</body>
<?php include("close.php"); ?>
</html>
-
TheBrandon
- Forum Commoner
- Posts: 87
- Joined: Tue May 20, 2008 8:55 am
Re: Including part of a file, not the whole file
The header needs to be multiple files, or not an include at all.syth04 wrote:Likewatson516 wrote:I don't believe you can easily include part of a file. An idea you could do would be to chop the html file up into smaller files and including them into that file and then whatever part you need, include just that file.
This would break up your one file into 4 files. Good idea for templates.Code: Select all
<?php include("header.php"); ?> <html> <body> <?php include("body.php"); ?> <?php include("footer.php"); ?> </body> <?php include("close.php"); </html>
You need unique Titles, descriptions, etc. on every page.
I made that mistake starting out.
Re: Including part of a file, not the whole file
You can simply have the dynamic data passed in as variables. If you do that though, I'd recommend you use some form of templating system as to not pollute the global scope.
Re: Including part of a file, not the whole file
TheBrandon wrote:The header needs to be multiple files, or not an include at all.syth04 wrote:Likewatson516 wrote:I don't believe you can easily include part of a file. An idea you could do would be to chop the html file up into smaller files and including them into that file and then whatever part you need, include just that file.
This would break up your one file into 4 files. Good idea for templates.Code: Select all
<?php include("header.php"); ?> <html> <body> <?php include("body.php"); ?> <?php include("footer.php"); ?> </body> <?php include("close.php"); </html>
You need unique Titles, descriptions, etc. on every page.
I made that mistake starting out.
Yes, this was just an example demonstration how one could do it.
Re: Including part of a file, not the whole file
Thanks for your replies. I've already applied your multi-including idea to make a template for my web site (man, it goes cleaner and cleaner!). However, I meant other thing.
Let's say I store all the <p>s I use in my web site in a single file called "writing.php". Those <p>s contain things like the welcome, description of services, about me's, whatever; each <p> corresponds to a page e.g. the welcome message will go to "index.php", the brief about me will go to "profile.php" and so on. Then I give each <p> an special mark (like the "id" in HTML) so I can include a given <p> in its respective page by referring to it from the page. Or not limited only to marks. Any way to include part of a file. I find it hard to explain.
I think that would be easier because I'd have all the content of the same type in a single file. If I get one file for each <p>, I'd have too many <p>s around to handle.
I apologize, Pytrin. I don't understand your reply. Could you please give an example?
Let's say I store all the <p>s I use in my web site in a single file called "writing.php". Those <p>s contain things like the welcome, description of services, about me's, whatever; each <p> corresponds to a page e.g. the welcome message will go to "index.php", the brief about me will go to "profile.php" and so on. Then I give each <p> an special mark (like the "id" in HTML) so I can include a given <p> in its respective page by referring to it from the page. Or not limited only to marks. Any way to include part of a file. I find it hard to explain.
I think that would be easier because I'd have all the content of the same type in a single file. If I get one file for each <p>, I'd have too many <p>s around to handle.
I apologize, Pytrin. I don't understand your reply. Could you please give an example?
Re: Including part of a file, not the whole file
You can substitute some of the content with variables. Example:
index.php
head.php
So $title is a variable passed in according to the page loaded. You don't need to include multiple files for different content, just pass in dynamic variables.
The problem with this simple example I showed here is that it is polluting the global scope - $title will be available inside body.php and footer.php as well, which might cause unforeseen problems.
To avoid that, use a simple templating system. There are plenty of those available, or you can write a simple one yourself - I gave an example of that on a recent post I wrote on my blog - http://www.techfounder.net/2008/11/18/o ... emplating/
index.php
Code: Select all
<?php
//Fetch page according to your control logic
$page = getPage($_GET['page']);
$title = $page['title'];
<html>
<?php include("head.php"); ?>
<body>
<?php include("body.php"); ?>
<?php include("footer.php"); ?>
</body>
</html>Code: Select all
<head>
<title><?php echo $title; ?></title>
</head>The problem with this simple example I showed here is that it is polluting the global scope - $title will be available inside body.php and footer.php as well, which might cause unforeseen problems.
To avoid that, use a simple templating system. There are plenty of those available, or you can write a simple one yourself - I gave an example of that on a recent post I wrote on my blog - http://www.techfounder.net/2008/11/18/o ... emplating/