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.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Custom PHP scripts


Reply


5 Posts   2847 Views

Avatar
ladrao2007

Community Member, 16 Posts

15 July 2008 at 4:42am

I have a question that hopefully someone can help with.. I am looking for a way to integrate some custom php/mysql scripts that I have to pull info from a database and display onto a page created by Silverstripe. To give a little background on the project, I've got a database with a bunch of store locations listed in it. I have a php script written to grab the states and display a list of the states, then when a state is clicked on it shows a list of cities, a city is clicked on and it shows the stores in that city. How would i go about creating a page/pages with the same similar functionality to it?

Any info or tips would be greatly appreciated. I am sure Silverstripe is capable at doing this, I'm just not sure how to go about it.

Thanks!

Avatar
Sam

Administrator, 685 Posts

18 July 2008 at 3:25pm

You should create a custom SilverStripe page type that has methods on it that call your custom code.

For example, here's a custom page type that accesses an external php file to get some code from that.

require_once('../mycode/something-custom.php'); // path is relative to sapphire/main.php
class CustomPage extends Page {
}

class CustomPage_Controller extends Page_Controller {
function SomeData() {
return my_custom_data_function();
}
}

You can then access $SomeData in your template:

<h1>Your data is here:</h1>
<p>$SomeData</p>

If you want to create structured data such as repeating elements, you should package data into ArrayData and DataObjectSet objects.

Avatar
ladrao2007

Community Member, 16 Posts

19 July 2008 at 6:22am

Ok, I created a CustomPage.php file and put that code inside of it, then created a CustomPage.ss file, and also created a file in mycode/custom.php. I put a function in that file that is just echo'ing out a string of text. When I go into the admin section and create a new custom page, everything shows up fine, but the string of text it is echo'ing out is being shown at the very top left of the page. I am just playing with the tutorial site right now and here is how i am displaying the content with the CustomPage.ss in the layout folder.

<div id="Content" class="typography">
<% if Level(2) %>
<div class="breadcrumbs">
$Breadcrumbs
</div>
<% end_if %>         
$Content
$SomeData
$Form
</div>


The content part is displaying correctly, but not any of the code from the custom function.

Any ideas?

Avatar
ladrao2007

Community Member, 16 Posts

22 July 2008 at 9:48am

Any suggestions why its not putting the text from the custom script inside the page where it should be?

Avatar
Willr

Forum Moderator, 5513 Posts

22 July 2008 at 1:43pm

I put a function in that file that is just echo'ing out a string of text.

So when the parser is parsing the file it will probably be getting to that function for some reason and echoing it. Rather then having your custom method echoing it, just return "the string" - the template parser will do all the echoing you need.