Page 1 of 1

Variables and include function help

Posted: Thu Dec 31, 2009 11:29 am
by steve grout
Guys,

I am attempting to write my 1st website in php. I have got so far but i have stumbled across a problem.

I wish to include a php file in another php file within a variable. (if that makes any sense!!)

Here is the code i am talking about:-

Code: Select all

<?php 
 
$site_title = "Official Website of the Invicta Dynamics";
$page_title = "Home of The Ladies Ice Hockey club for Kent South-East UK";
$main .= "<h2>Welcome to our Hockey site</h2>
          <p>Glad you dropped by and hope you find our website an enjoyable and exciting experience! You've successfully found your way to our home page.</p>      
          <p>  include ('../../plogger-1.0RC1/plog-content/plugins/random-images/random-images.php') </p>";
 
require('admin/template.php'); 
?>

The file i am trying to include is on line7. All works find except this section.

I am very new so may have to be basic with myself.

Thanks in advance

Steve

Re: Variables and include function help

Posted: Thu Dec 31, 2009 1:44 pm
by AbraCadaver
Assuming random-images.php outputs an image tag, then here's one way to do it:

Code: Select all

//start an ouput buffer
ob_start();
//include the file that outputs stuff - the buffer will catch this
include ('../../plogger-1.0RC1/plog-content/plugins/random-images/random-images.php');
//assign the contents of the buffer to a variable and delete and stop the buffer
$random_image = ob_get_clean();
 
$main .= "<h2>Welcome to our Hockey site</h2>
          <p>Glad you dropped by and hope you find our website an enjoyable and exciting experience! You've successfully found your way to our home page.</p>      
          <p> $random_image </p>";
//use the var above
If you are going to do this a lot, then you might build a function:

Code: Select all

function getFileOutput($file) {
    ob_start();
    include ($file);
    return ob_get_clean();
}
 
$random_image = getFileOutput('../../plogger-1.0RC1/plog-content/plugins/random-images/random-images.php');