Simple PHP block

Permalink 2 users found helpful
Hi,

I've made a simple block that executes PHP code within it. You can write your own PHP code such as formatting a date, querying data from different tables, or inserting a jQuery script etc.

The block uses php function eval() to evaluate code string. So, please do not put any php opening/closing tag '<?' or '<?php'.

Example of what you can put in the editable area:

$d = date("d-m-Y");
$str = <<<EOT
<p class="date">$d</p>

EOT;
echo $str;

Enjoy.
Dang.

1 Attachment

View Replies:
simpled replied on at Permalink Reply
great stuff dang, thank you
froggie81 replied on at Permalink Reply
Works great , JavaScript behaves as well. Should be part of the core offering.

Thanks for this.

Frank G.
Iconoclast replied on at Permalink Reply
I'm trying to get this to work in on my page so I can include a random quote content block...here's the PHP script I have:

$quote = array(
1  => "a",
2  => "b",
3  => "c",
4  => "d",
5  => "e",
);
srand ((double) microtime() * 1000000);
  $randnum = rand(1,5);
echo"$quote[$randnum]";


But when I put then in my editable content block, it just treats it like text/HTML...am I doing something wrong? Please help!
frz replied on at Permalink Reply
frz
You'd have to download and install the block jpallian attached to his original post above - otherwise you're still posting PHP to the Content Block which is going to still not work.

Here's how you install a block:
http://www.concrete5.org/help/editing/installing_a_block/...
Iconoclast replied on at Permalink Reply
Aha, that works perfectly, thanks so much! :D
tex replied on at Permalink Reply
This block looks _so_ appealing, and I really want to get it in place. I've downloaded the zip archive and placed the contents at concrete/blocks/php_code, but I don't see any new option to install the block when I go to Dashboard --> Add Functionality. It just isn't there. The file permissions look fine (consistent with other directories under blocks/). Does anyone have any ideas? I'm completely new to Concrete but trying to get up to speed.

FYI, I'm trying to implement the 'image block' feature from gallery2. I think the php block is going to make that possible, but please chime in if you've got an alternative recommendation. Thanks!
ipallian replied on at Permalink Reply
[site folder]/concrete/blocks/ is for Concrete core blocks only.

See this:http://www.concrete5.org/help/building_with_concrete5/developers/un...
tex replied on at Permalink Reply
Thank you for the prompt reply. I had read that link but totally missed the /blocks vs /concrete/blocks issue - should have read it more carefully. All good now. Thank you so much!
s2d replied on at Permalink Reply
s2d
I've got all kinds of uses for this block. Thanks very much for sharing it.
mario replied on at Permalink Reply
mario
this will come in useful!
hivoltage replied on at Permalink Reply
Hey, thanks, this will come in useful.

Have found a bug though ...

The PHP block does not retain it's positioning after editing the block. By this I mean, if you were to add a PHP Block to your page (that has other blocks) then move the PHP block to the top of the page (save position), then edit the PHP block ... after saving the PHP block it jumps back to the bottom of the page and is no longer the first block on the page.
izne replied on at Permalink Reply
wouldnt it be nice if we can edit this php block in the source editors fashion? some source coloring and font-size posibility for instance?
Remo replied on at Permalink Reply
Remo
html and javascript aren't really great to build text editors with syntax highlighting. there was a contest about it and even the winner built a not really useful editor.

Due to the archtitecture of c5, you're not supposed to built a complete page using such a block. you should never have lots of code lines there and I just think it's not worth the effort at all..

but feel free to implement it..
izne replied on at Permalink Reply
anyway, i am writing few lines of php in this nice little block but still i find the textarea uncomfortable.
so yeah of course i will drop something more decent. i wasnt sure if there is anything like this which does source-coloring. obviously not :)
have you seen the amy editor?
http://www.amyeditor.com/
Remo replied on at Permalink Reply
Remo
I'm not saying the textarea is comfortable, I'm just saying that you might consider using a different approch if you're using the php block very often.

Never seen amyeditor before, but I can't see any syntax highlighting expect the export function..?
izne replied on at Permalink Reply
well not very often but i need it from time to time to make small scripts to do nice things. and exactly then it is uncomfortable to look at that textarea, even if i have to put 4 lines of code.

i cant understand how you cant see source highlighting in the amy editor.
frz replied on at Permalink Reply
frz
I can't help but to think that it'd be worth your while to make these scripts into blocks, part of a template file in a theme, or single pages depending on their purpose. I dunno, I get the need to do a quickie date lookup or something, but in general the php block is not something that we'd expect to use - so you're not going to see it turn into a code editor with highlighting and indention and whatnot.. .
ipallian replied on at Permalink Reply
I still need this block from time to time (e.g I have to pull data from another site using curl and display on my site). It's a bit technical block so i agree with 'frz' that it's not a good idea to make it part of C5 blocks.

@hivoltage: I did a quick test to move the block around the it saves the position fine. BTW, it's C5 feature to save the position, not the block's.

@izne: I dunno about the amy editor neither. How about you implement it?
izne replied on at Permalink Reply
i already did it - turned it to a source editor, will post the modified block here soon (if the author is ok). it is WAY better with source highlighting.

i agree i can put php in many different ways, and i still like this one - using this block... i just add php block - > write some code -> done. :)
ipallian replied on at Permalink Reply
waiting for your block....
synlag replied on at Permalink Reply
synlag
has it auto-completion too? ;)
tuukka replied on at Permalink Reply
PHP block shows in the preview mode OK, but when the code is committed, I get the error. Any suggestions?

Fatal error: method_exists() [<a href='function.method-exists'>function.method-exists</a>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition &quot;PHPCodeBlockController&quot; of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in homepath/concrete/libraries/database_indexed_search.php on line 135
primewaydesign replied on at Permalink Reply
primewaydesign
how can i include external php pages using this block???
simon8or replied on at Permalink Reply
I'm also interested to know if we can use external php pages. I tried using:

include("subdirectory/page.php");

But all I get is erros.
djmcgreal replied on at Permalink Reply
In contoller.php rename "PHPCodeBlockController" to "PhpCodeBlockController" so that line 8 becomes:

?><?php class PhpCodeBlockController extends BlockController {


This will fix the error which is caused by the camel-case parseing of the block (i.e. php_code directory looks for an object called PhpCodeBlockController). If you don't want to change the code renaming the directory to p_h_p_code might work, though you'll have to reinstall it. Also, it will work if you disable the Basic Cacheing in Sitewide Settings within the Dashboard.

Best,
Dan.
tex replied on at Permalink Reply
I love this block and have been using it for over a year (see earlier post), but it seems to be producing errors when I upgrade from 5.4.0.5 to 5.4.1. I get the following error after upgrading to 5.4.1, but it's fine when I revert to 5.4.0.5. Does anyone have any advice? Thanks!

Fatal error: Page::supportsPageCache() [<a href='page.supportspagecache'>page.supportspagecache</a>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition &quot;PHPCodeBlockController&quot; of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in /home1/mysite/public_html/concrete/models/page.php on line 2089
AlexDuchovny replied on at Permalink Reply
AlexDuchovny
Same here. I have a pretty simple block(fetching some data from a db and showing it) but after upgrade to 5.4.1.1 it produces:

Fatal error: Page::supportsPageCache(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "CarSearchBlockController" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in .../concrete/models/page.php on line 2099

I'd be very thankful for any help.
nbruley replied on at Permalink Reply
Would this block work if I have created a JavaScript variable elsewhere on the page and wish to use that variable value in PHP?
nageeb replied on at Permalink Reply
This block is all kinds of awesome. As someone who's just starting out in Concrete5, it's a lifesaver for migrating legacy php functionality into C5. Thank you!
nbruley replied on at Permalink Reply
I installed this block once on one of my sites and it worked great. Now I went to install it on a second site and it is no longer in the community listing as far as I can see so I had to copy the folder manually into my blocks folder. It showed up and I said to install it. But now when I try to add a php block, even an empty one, it just keeps grinding away and doesn't finish adding the block. I'm not sure it installed correctly because there was basically no hesitation when installing. What might I have done wrong? Thanks.
nbruley replied on at Permalink Reply
Hmmm, logged out, logged back in and works. Good to go.
jazzypants replied on at Permalink Reply
Hi, I have unzipped the file and placed it in the blocks folder and its not appearing when i click add functionality. i have pretty much placed it in every folder that exists now and i just cant get it to appear.. What am i doing wrong? this is concrete 5.5.1 if that helps