21297 Posts in 5734 Topics by 2603 members
General Questions
SilverStripe Forums » General Questions » Inlude external php script in Page.ss / Page.php - Template overwritten
General questions about getting started with SilverStripe that don't fit in any of the categories above.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 1790 Views |
-
Inlude external php script in Page.ss / Page.php - Template overwritten

4 May 2010 at 10:53pm Last edited: 4 May 2010 11:22pm
Hello,
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 Page.ssIn 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?Ty!
-
Re: Inlude external php script in Page.ss / Page.php - Template overwritten

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: http://www.php.net/manual/en/function.ob-get-contents.php -
Re: Inlude external php script in Page.ss / Page.php - Template overwritten

4 May 2010 at 11:51pm Last edited: 4 May 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:
ob_start();the rest of the php code
at the bottom:
$out1 = ob_get_contents();
ob_end_clean();
var_dump($out1, $out2);Or how does it work exactly?
Because that didnt work. -
Re: Inlude external php script in Page.ss / Page.php - Template overwritten

5 May 2010 at 4:11am Last edited: 5 May 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() {
ob_start();
include (Director::baseFolder() . '/test/dealerlocator.php');
$output = ob_get_contents();
ob_end_clean();
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.
-
Re: Inlude external php script in Page.ss / Page.php - Template overwritten

6 May 2010 at 2:03am Last edited: 6 May 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() {
session_start();
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: $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 p.nz 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 $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: $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 $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) -
Re: Inlude external php script in Page.ss / Page.php - Template overwritten

6 May 2010 at 6:04pm
Hi
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 ;)
| 1790 Views | ||
|
Page:
1
|
Go to Top |

