Page 1 of 1

Including part of a file, not the whole file

Posted: Thu Dec 18, 2008 12:30 pm
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.

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

Posted: Thu Dec 18, 2008 12:57 pm
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.

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

Posted: Thu Dec 18, 2008 1:16 pm
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.

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

Posted: Thu Dec 18, 2008 1:19 pm
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.

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

Posted: Thu Dec 18, 2008 1:25 pm
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.

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

Posted: Thu Dec 18, 2008 1:28 pm
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.

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

Posted: Thu Dec 18, 2008 8:15 pm
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?

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

Posted: Thu Dec 18, 2008 8:29 pm
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/