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

PHP in Page.ss and including an external Site with include


Go to End
Reply


9 Posts   3935 Views

Avatar
Macello

Community Member, 37 Posts

2 October 2008 at 9:45am

Edited: 02/10/2008 9:54am

I already searched in the Forum but didn't found something similar.

There are 2 Problems i got:

1. i want to put an image changer in my Page, that looks like this in PHP:

<?php
$season = date("n");

if ($season <= "3" || $season >= "10") {
   $season = "<img src= \"winter.png\" alt=\"winter\"/> Winter ";
}
else {
   $season = "<img src= \"sommer.png\" alt=\"sommer\"/>Sommer ";
}
echo $season;
?>

Now how can i include this in my Silverstripe Page?

2. Very similar problem, i want to include in a div, with PHP "include" a html/php Site from another host. That means i want to create a sort of html Banner which i can include in several sites, so i can make changes on my host and on every site i made the changes will take effect too:

<?php
include("www.sample.ch/banner/index.php")
?>

can please someone tell me some step by step instructions how to implement these functions into the page.php and get them in to my Page.ss?

Hopefully getting some fast and nice answers ;)

Thank you Silverstripers! ;)

Avatar
Willr

Forum Moderator, 5513 Posts

2 October 2008 at 1:29pm

in Page.php in the Page_Controller section you can do this

function SeasonImage() {
$season = date("n");
if ($season <= "3" || $season >= "10") {
$season = "<img src= \"winter.png\" alt=\"winter\"/> Winter ";
}
else {
$season = "<img src= \"sommer.png\" alt=\"sommer\"/>Sommer ";
}
return $season;
}

Then in your page.ss template you just need to have $SeasonImage whereever you want!.

#2 Im a little less sure that this will work. What is in the PHP file on the server? Does it return HTML or just 'echo' it. If it echos it you are going to run into problems with SilverStripe with it eg if the script does 'echo "OMG this is cool";' it will not work within SilverStripe. Instead you will need a script which does 'return "OMG this is cool"'

Avatar
Macello

Community Member, 37 Posts

2 October 2008 at 7:45pm

Edited: 02/10/2008 7:56pm

Thank you for your quick answer! i will try out this today.

#2 Yes the script does echo html, itz a simple html/php/css banner with random links. do you have an idea how to implement an external page into Page.ss? it does not need to correspond with the CMS. or is it maybe possible with Virtualpage?

Greetz

Avatar
banal

Community Member, 901 Posts

10 October 2008 at 5:07am

Question #2: What about a iframe?

Avatar
Macello

Community Member, 37 Posts

13 October 2008 at 2:42am

I prefer using PHP, than iFrame, could be a solution anyway...
my script works with print too, could this help? how to include this "file.php" in Page.

<html>
   <head>
      <title></title>
   </head>
   <body>
   <?php

$data = file("db.txt");

srand((float)microtime() * 1000000);

shuffle($data);

foreach ($data as $line)
{
list($link, $image, $name, $slogan) = explode(":", $line);
print "<a href='http://$link'><img src='$image.png'><img src='$image"."color.png'>$name</a>";
}
?>
   </body>
</html>

i want to have this on my server and link it on several sites in the Page.ss

Thanks for your help

Avatar
banal

Community Member, 901 Posts

13 October 2008 at 4:06am

Edited: 13/10/2008 4:07am

For php scripts that are located on a external server, a iframe is most likely the easiest solution (as long as it's a standalone script that doesn't provide functionality for your local code). Most of the hosting providers disable the inclusion of external php script due to security reasons.

Concerning your other question, you could rewrite your code to something like that:

// place this somewhere in your Page Class (Page.php)
function RandomImages() {
$data = file("db.txt");

srand((float)microtime() * 1000000);

shuffle($data);
$output = '';
foreach ($data as $line){
list($link, $image, $name, $slogan) = explode(":", $line);
$output .= "<a href='http://$link'><img src='$image.png'><img src='$image"."color.png'>$name</a>";
}

return $output;
}

In your template you then simply call
$RandomImages

That's it.

Avatar
Macello

Community Member, 37 Posts

16 October 2008 at 11:17pm

Edited: 17/10/2008 1:07am

Hello, thanks for all your help, but actually i can't use i frame, because the page i want to include looks like that:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Your title</title>
<link rel="stylesheet" type="text/css" media="screen" href="../Scripts/style.css"/>
<script type="text/javascript" src="../Scripts/mootools.svn.js"></script>
<script type="text/javascript">
      window.addEvent('domready', function(){
         /* Tips 1 */
         var Tips1 = new Tips($$('.Tips1'));
         
         /* Tips 2 */
         var Tips2 = new Tips($$('.Tips2'), {
            initialize:function(){
               this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
            },
            onShow: function(toolTip) {
               this.fx.start(1);
            },
            onHide: function(toolTip) {
               this.fx.start(0);
            }
         });
         
         /* Tips 3 */
         var Tips3 = new Tips($$('.Tips3'), {
               showDelay: 400,
               hideDelay: 400,
               fixed: true
         });

         
         /* Tips 4 */
         var Tips4 = new Tips($$('.Tips4'), {
            className: 'custom'
         });
      });
   </script>
</head>
<body>
<div class="container">
Affiliate83
<?php

$data = file("db.txt");

srand((float)microtime() * 1000000);

shuffle($data);

foreach ($data as $line)
{
list($link, $image, $name, $slogan) = explode(":", $line);
print "&nbsp;<a href=\"http://$link\" target=\"_blank\"><img src=\"http://$link/favicon.ico\" height=\"20px\" width=\"20px\" border=\"0px\" padding-top=\"5px\" class=\"Tips2\" title= '$name | <img src=\"http://$link/favicon.ico\" height=\"30px\" width=\"200px\"> <br> $slogan'></a>";
}
?>
</div>
</body>
<html>

the Javascript "tip" doesn't work right because the i'frame is only 30px high, so it covers the tip. as you can see [url=http://www.studio83.ch/Scripts/code.php]here[/url],
the upper is included with iframe, the lower with include, and shows right.

Any Suggestions how i could do this? i can't put this up to date on every site, what i want is beeing able to edit the content and style by editing the bannersite on my server to bring it up to date on every other site, including the banner.

THX Marcello

Avatar
banal

Community Member, 901 Posts

20 October 2008 at 8:23pm

There are different solutions for this. You could create a bigger iframe and set its background to [url=http://www.tech-recipes.com/rx/1253/htmlcss-transparent-iframes-in-all-browsers/]transparent[/url]. Or you simply create a JavaScript file that inserts a div container into your target site and loads the contents using AJAX. That is probably the most elegant solution, since you'll only have to include a JavaScript file on your target sites (plus dependencies of course).

Go to Top