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.

Customising the CMS /

How to convert this PHP page to a SilverStripe page?


Go to End
Reply


9 Posts   2441 Views

Avatar
Brig

Community Member, 26 Posts

28 July 2009 at 3:51am

Edited: 28/07/2009 3:52am

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

Avatar
Brig

Community Member, 26 Posts

28 July 2009 at 9:40pm

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?

Avatar
Willr

Forum Moderator, 5513 Posts

28 July 2009 at 10:08pm

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);
..

Avatar
Brig

Community Member, 26 Posts

28 July 2009 at 10:17pm

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.
[url]http://nda.johannesengstrom.com/guildroster/roster.php[/url]

Avatar
Willr

Forum Moderator, 5513 Posts

28 July 2009 at 10:20pm

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.

Avatar
Brig

Community Member, 26 Posts

28 July 2009 at 10:22pm

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.

Avatar
Brig

Community Member, 26 Posts

28 July 2009 at 10:59pm

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.

Avatar
Brig

Community Member, 26 Posts

28 July 2009 at 11:02pm

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 :(

Go to Top