Including part of a file, not the whole file

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

Post Reply
ocetalo
Forum Newbie
Posts: 2
Joined: Thu Dec 18, 2008 12:18 pm

Including part of a file, not the whole file

Post by ocetalo »

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.
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Including part of a file, not the whole file

Post by watson516 »

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.
syth04
Forum Newbie
Posts: 14
Joined: Thu Dec 18, 2008 12:12 am

Re: Including part of a file, not the whole file

Post by syth04 »

watson516 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.
Like

Code: Select all

 
<?php include("header.php"); ?>
<html>
<body>
<?php include("body.php"); ?>
<?php include("footer.php"); ?>
</body>
<?php include("close.php"); ?>
</html>
 
This would break up your one file into 4 files. Good idea for templates.
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

Re: Including part of a file, not the whole file

Post by TheBrandon »

syth04 wrote:
watson516 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.
Like

Code: Select all

 
<?php include("header.php"); ?>
<html>
<body>
<?php include("body.php"); ?>
<?php include("footer.php"); ?>
</body>
<?php include("close.php");
</html>
 
This would break up your one file into 4 files. Good idea for templates.
The header needs to be multiple files, or not an include at all.

You need unique Titles, descriptions, etc. on every page.

I made that mistake starting out.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Including part of a file, not the whole file

Post by Eran »

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.
syth04
Forum Newbie
Posts: 14
Joined: Thu Dec 18, 2008 12:12 am

Re: Including part of a file, not the whole file

Post by syth04 »

TheBrandon wrote:
syth04 wrote:
watson516 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.
Like

Code: Select all

 
<?php include("header.php"); ?>
<html>
<body>
<?php include("body.php"); ?>
<?php include("footer.php"); ?>
</body>
<?php include("close.php");
</html>
 
This would break up your one file into 4 files. Good idea for templates.
The header needs to be multiple files, or not an include at all.

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.
ocetalo
Forum Newbie
Posts: 2
Joined: Thu Dec 18, 2008 12:18 pm

Re: Including part of a file, not the whole file

Post by ocetalo »

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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Including part of a file, not the whole file

Post by Eran »

You can substitute some of the content with variables. Example:

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>
head.php

Code: Select all

<head>
   <title><?php echo $title; ?></title>
</head>
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/
Post Reply