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.
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.
great stuff dang, thank you
Works great , JavaScript behaves as well. Should be part of the core offering.
Thanks for this.
Frank G.
Thanks for this.
Frank G.
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:
But when I put then in my editable content block, it just treats it like text/HTML...am I doing something wrong? Please help!
But when I put then in my editable content block, it just treats it like text/HTML...am I doing something wrong? Please help!
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/...
Here's how you install a block:
http://www.concrete5.org/help/editing/installing_a_block/...
Aha, that works perfectly, thanks so much! :D
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!
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!
[site folder]/concrete/blocks/ is for Concrete core blocks only.
See this:http://www.concrete5.org/help/building_with_concrete5/developers/un...
See this:http://www.concrete5.org/help/building_with_concrete5/developers/un...
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!
I've got all kinds of uses for this block. Thanks very much for sharing it.
this will come in useful!
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.
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.
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?
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..
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..
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/
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/
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..?
Never seen amyeditor before, but I can't see any syntax highlighting expect the export function..?
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.
i cant understand how you cant see source highlighting in the amy editor.
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.. .
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?
@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?
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. :)
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. :)
waiting for your block....
has it auto-completion too? ;)
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 "PHPCodeBlockController" 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
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 "PHPCodeBlockController" 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
how can i include external php pages using this block???
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.
include("subdirectory/page.php");
But all I get is erros.
In contoller.php rename "PHPCodeBlockController" to "PhpCodeBlockController" so that line 8 becomes:
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.
?><?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.
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 "PHPCodeBlockController" 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
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 "PHPCodeBlockController" 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
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.
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.
Would this block work if I have created a JavaScript variable elsewhere on the page and wish to use that variable value in PHP?
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!
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.
Hmmm, logged out, logged back in and works. Good to go.
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