PHP Block works in Draft not Approved

Permalink
Hi

I can't seem to find my exact problem on the forum, so I'm hoping someone will be able to help. I have a PHP code block on my site which attempts to load two page attributes and pass them to a URL which generates a charity totaliser. When I test it in Draft it works OK, but as soon as I approve it, the result is an error - An unexpected error occurred. Call to a member function getAttribute() on null

It feels like a permissions thing, but I'm admin.

My code is simple as I have never used PHP before, it's a steep learning curve!

<?php 
    $ProjectProfit = $c->getAttribute('project_total_raised');
    $ProjectTotal = $c->getAttribute('project_total');
    if($ProjectTotal){
        echo '<div style="text-align:center;"><img border="0" src="http://www.coolfundraisingideas.net/thermometer/thermometer.php?currency=pound&goal='.$ProjectTotal.'&current='.$ProjectProfit.'&color=red&size=medium"></p></div>';
}?>

 
drgreenway replied on at Permalink Reply
PS, I put the if statement in there to try to stop it throwing such a monumental error, when it happens, I can no longer edit the block and have to revert to an earlier version of the page. It didn't work. I tried the if statement on both page attributes, and both still worked in draft.
Gondwana replied on at Permalink Best Answer Reply
Gondwana
I'd say that $c isn't be instantiated in this context. You could try to precede it with something like:
$c = Page::getCurrentPage();
drgreenway replied on at Permalink Reply
That's sorted it. Took a while to get back to this problem. Thanks!