Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Custom PHP scripts

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

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

Page: 1
Go to End
Author Topic: 2807 Views
  • ladrao2007
    Avatar
    Community Member
    16 Posts

    Custom PHP scripts Link to this post

    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!

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Custom PHP scripts Link to this post

    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.

  • ladrao2007
    Avatar
    Community Member
    16 Posts

    Re: Custom PHP scripts Link to this post

    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?

  • ladrao2007
    Avatar
    Community Member
    16 Posts

    Re: Custom PHP scripts Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Custom PHP scripts Link to this post

    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.

    2807 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.