Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » How to convert this PHP page to a SilverStripe page?

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

Page: 1 2
Go to End
Author Topic: 2220 Views
  • Brig
    Avatar
    Community Member
    26 Posts

    How to convert this PHP page to a SilverStripe page? Link to this post

    Hello

    Could someone please help me sort this out. I've been spending too much time trying to solve this already
    All other issues I've had the tutorial or the forum have been able to solve it for me but not this time.

    I have a PHP page that I need to convert into a SilveStripe page. I've understood that any PHP code needs to go in the page controller class but my PHP page got HTML in it as well and I'm not sure how to separate it.

    I have attached the standalone PHP page as it's quite large. How can I convert this to a page controller and ss page please?

    Really hoping someone can help me as this is the last page on my new site.

    Thanks a lot

    Attached Files
  • Brig
    Avatar
    Community Member
    26 Posts

    Re: How to convert this PHP page to a SilverStripe page? Link to this post

    Ok have done some more research and have found something that I think should work but it doesn't yet. I'm trying to get content from a second MySQL database on my server, not the silverstripe database.

    I created this function in my page controller:

    function Members() {
             $db = new MySQLDatabase(array("server" => "localhost","username" => "*****","password" => "******","database" => "******" ));
             
          $query = $db->query("SELECT * FROM roster ORDER BY Class ASC, Name ASC");
             $result = new DataObjectSet();
             foreach($query as $row) {
             $result->push(new ArrayData($row));
          }
             return $result;
          }

    Then in my SS template I put $Members but I get no results.

    Any idea what I'm doing wrong?

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: How to convert this PHP page to a SilverStripe page? Link to this post

    Then in my SS template I put $Members but I get no results.

    Does your query return any results?

    ..
    $result = new DataObjectSet();
    print_r($result);
    ..

  • Brig
    Avatar
    Community Member
    26 Posts

    Re: How to convert this PHP page to a SilverStripe page? Link to this post

    Thanks for the reply Willr. Where am I supposed to see the result when I put the print_r line in the code?

    Sorry I'm in way over my head with this but really need to get it to work.

    It works fine in it's own standalone PHP page, can be seen here so it's nothing wrong with the actual queries.
    http://nda.johannesengstrom.com/guildroster/roster.php

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: How to convert this PHP page to a SilverStripe page? Link to this post

    print_r should output the results of your query at the top of your page. If you see no queries (You will know if its there) then your template is not calling that function correctly.

  • Brig
    Avatar
    Community Member
    26 Posts

    Re: How to convert this PHP page to a SilverStripe page? Link to this post

    Ah I see. Will play around with it for a bit. Thanks a lot this should at least help me get the call to work.

  • Brig
    Avatar
    Community Member
    26 Posts

    Re: How to convert this PHP page to a SilverStripe page? Link to this post

    Still can't get it to work. It doesn't return anything on the page even with the print_r.

    Here's my PHP page controller:

    <?php
    /**
    * Defines the Roster page type
    */
    class Roster extends Page {
    static $db = array(
    );
    static $has_one = array(
    );

    }

    class Roster_Controller extends Page_Controller {
       
       /**
       * Guild Memebers - Returns all members
       */
          function Members() {
             $db = new MySQLDatabase(array("server" => "localhost","username" => "****","password" => "****","database" => "****"));
             
             $query = $db->query("SELECT * FROM roster ORDER BY Class ASC, Name ASC");
                $result = new DataObjectSet();
                print_r($result);
                foreach($query as $row) {
                $result->push(new ArrayData($row));
                }
                   //return $result;
             }

    }

    ?>

    And here is the SS page:

    <div class="typography-site">

          <div id="NewsContentSingle" class="typography-site">
             <% include BreadCrumbs %>
             
                <h1>$Title</h1>
                   $Content
                   
                   <p>$Members</p>
                   
    </div>
    </div>

    The query isn't at fault as I'm using the exact same query in a different page outside of SilverStripe. Seems like my SS template just isn't picking up the function.

    I don't get any error messages. The dev/build?flush=1 runs as expected.

    Can you spot anything that looks like it could cause it to fail.

  • Brig
    Avatar
    Community Member
    26 Posts

    Re: How to convert this PHP page to a SilverStripe page? Link to this post

    Oh spoke too soon. Now I do get something on the page with the print_r.

    DataObjectSet Object ( [items:protected] => Array ( ) [odd:protected] => 0 [first:protected] => 1 [last:protected] => [current:protected] => [pageStart:protected] => [pageLength:protected] => [totalSize:protected] => [paginationGetVar:protected] => start [iteratorPos:protected] => [iteratorTotalItems:protected] => [failover:protected] => [_object_cache:protected] => Array ( ) [_xml_cache:protected] => Array ( ) [_natural_cache:protected] => Array ( ) [customisedObj:protected] => [parent:protected] => [namedAs:protected] => [class] => DataObjectSet [extension_instances:protected] => Array ( ) )

    I'm not sure what this means though

    2220 Views
Page: 1 2
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.