Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Inlude external php script in / Page.php - Template overwritten

Go to End

6 Posts   3529 Views


Community Member, 267 Posts

4 May 2010 at 10:53pm

Edited: 04/05/2010 11:22pm


I'm trying to add an existing php script (DealerLocator script) into my template, but it isn't going as planned.
Used the forum searched and tried those solutions. At the moment I have '$IncludeDealerLocator' in my

In mysite/code/page.php (in the controller part)

	function IncludeDealerLocator(){
		require_once(Director::baseFolder() . '/test/dealerlocator.php');

But now, when I open the page, the website only shows the php file, and not the whole template with the php script in a box.
So please tell me: what is te best way to include a php script into the template?



Community Member, 904 Posts

4 May 2010 at 11:26pm

That's because the stuff that's inserted at $IncludeDealerLocator in your template is the return value of your function. Simply including an external file will do something completely different (include the file when the function is being executed).
You can use PHPs output buffering capabilities to capture the PHP output buffer and return it from your function. Here's the PHP reference regarding output buffers:


Community Member, 267 Posts

4 May 2010 at 11:51pm

Edited: 04/05/2010 11:55pm

Hi Banal,

Thanks for replying. If I understand you correctly I must add to the php file that I include:

on line 1:

the rest of the php code

at the bottom:
$out1 = ob_get_contents();
var_dump($out1, $out2);

Or how does it work exactly?
Because that didnt work.


Community Member, 904 Posts

5 May 2010 at 4:11am

Edited: 05/05/2010 4:13am

Hi borriej

You don't have to change your included file. Your function in the controller is wrong.
Your function should look like this:

public function IncludeDealerLocator() {
	include (Director::baseFolder() . '/test/dealerlocator.php');
	$output = ob_get_contents();
	return $output;

Notice: This is some sort of a hack and should only be used when there's absolutely no other way to do it. Usually you would solve stuff like this with custom template-includes or use "renderWith" methods to render data with a given template.


Community Member, 267 Posts

6 May 2010 at 2:03am

Edited: 06/05/2010 2:06am

Hi Banal,

I am trying to do it the SilverStripe method now, because I want to code to be clean and I want to output to be shown in the content div and not in the same box on the right.

When I do this:

		public function IncludeDealerLocator() {
				require_once(Director::baseFolder() . '/test/dbconnect.php');
				if ($_POST['address_submit'] == "1")  
					if ((strlen($_POST['postcode']) == "6") && (substr($_POST['postcode'], 0, 4) >= "1000") && (substr($_POST['postcode'], 0, 4) <= "9999") && (eregi("([a-z])", substr($_POST['postcode'], 4, 2)))) 
						$pc = $_POST['postcode'];
						$query1 = "SELECT OW, NZ FROM hbt_postcodes WHERE postcode = '$pc'";
						$result1 = mysql_query($query1) or die('Fout : ' . mysql_error());
						while($row = mysql_fetch_array($result1, MYSQL_ASSOC))
							$pc1 = $row['postcode'];
							$OW  = $row['OW'];
							$NZ  = $row['NZ'];
							print "<div class=groen>Ingegeven postcode:&nbsp;&nbsp;$pc</div>";
							print "<br>";			
						$OWp = $OW + (10*0.0146); 
						$OWm = $OW - (10*0.0146);
						$NZp = $NZ + (10*0.009); 
						$NZm = $NZ - (10*0.009); 
						$query2 = "SELECT * FROM hbt_postcodes AS p, Dealer AS d WHERE d.postcode=p.postcode AND BETWEEN $NZm AND $NZp AND p.ow BETWEEN $OWm AND $OWp";
						$result2 = mysql_query($query2) or die('Fout : ' . mysql_error());
						$aantal = mysql_num_rows($result2);

						print ("Er werd(en) $aantal record(s) gevonden<br><br>");		
							while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) 
								$naam2  = $row2['naam'];
								$adres2 = $row2['adres'];
								$postc2 = $row2['postcode'];
								$plaat2 = $row2['plaats'];
								$telef2 = $row2['telefoon'];
								$web2   = $row2['website'];
								print "<div class=vetrood>$naam2</div>";
								print "<div class=groen>$adres2, $postc2&nbsp;&nbsp;$plaat2, $telef2";
								if ($web2 !='') { print ", <a href=\"http://$web2\">$web2</a>"; }
								print "</div><br />";
					} elseif ($_POST['plaats_submit'] == "1") {	 // ZOEKEN OP PLAATSNAAM
						$plaats = $_POST['plaats'];	
						$query = "SELECT * FROM Dealer WHERE plaats LIKE '%". $_POST['plaats'] . "%'";
						$result = mysql_query($query) or die ("Fout: ". mysql_error());
						$aantal = mysql_num_rows($result);
						print "<div class=groen>Ingegeven postcode:&nbsp;&nbsp;$plaats</div>";
						print "<br>";
						print ("Er werd(en) $aantal record(s) gevonden<br><br>");
							while($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
								$naam  = $row['naam'];
								$adres = $row['adres'];
								$postc = $row['postcode'];
								$plaat = $row['plaats'];
								$telef = $row['telefoon'];
								$web   = $row['website'];
								print "<div class=vetrood>$naam</div>";
								print "<div class=groen>$adres, $postc&nbsp;&nbsp;$plaat, $telef";
								if ($web !='') { print ", <a href=\"http://$web\">$web</a>"; }
								print "</div><br />";
					} else {
						print "<div><p>Zoek op postcode:</p></div>";
						print "<div><form method=\"post\">";
						print "<input type=\"hidden\" name=\"address_submit\" value=\"1\">";
						print "<p><input type=\"text\" name=\"postcode\" maxlength=\"6\"></p></div>";
						print "<div><input type=\"submit\" value=\"Zoek\">\n";
						print "</form>\n</div>";
						print "<p>Zoek op plaats:</p>";
						print "<form method=\"post\">\n";
						print "<input type=\"hidden\" name=\"plaats_submit\" value=\"1\">\n";
						print "<p><input type=\"text\" name=\"plaats\" maxlength=\"50\"><br>\n</p>";
						print "<input type=\"submit\" value=\"Zoek\">\n";
						print "</form>\n";

The dealer locator is just at the top of the page and not even in it's div where the variable is.

So I need to do something like:

$output = all the code above
return $output=>renderWith ('Page')

or how does this work?
I want to search forms at the right side of the page in my div,
and the output must be in the middle (content div)


Community Member, 904 Posts

6 May 2010 at 6:04pm


I'm really sorry, but that's not really the SilverStripe way of doing things. Problems start with session_start, inclusion of DB connection info and end with print statements. You don't need all these things. SilverStripe can manage the session and db connections for you. You don't need print statements, because that's where you would use templates.

I'm afraid it would take too long to explain how this should be done properly. Instead I suggest you start with the SilverStripe tutorials. They cover a lot of the essentials. Also read about templates and the SilverStripe datamodel.
Or just buy the SilverStripe book ;)