HomeNotesHow to Add PHP in a BLOCK

How to Add PHP in a BLOCK

Extend Your Blocks with the Power of PHP!

It seems to have finally happened. You can now add PHP in a Block as of 612, but with one caveat. You have to make an entry in the PageCompose table and you have to add your PHP via phpMyAdmin. This shouln't be too hard as it's a lot easier now than before.

Here's a simple example:

The above example uses an echo function. For example:


echo "Look at me I'm the PHP!';


Here's one that uses a Flash chart and PHP. There's a few additional code that I had to write, but it's the same concept.

BoonexNerd.com - Dolphin Tutorial on How to Add PHP in a Block



So what has changed? Well take a look at this:

function getBlockCode_PHP( $iBlockID, $sContent ) {
ob_start();
eval($sContent);
return ob_get_clean();
}


The function can be found in the BxDolPageView.php file located in inc/classes. That new function was introduced in 612 and as such it will parse PHP code. Comapre that to the typical ECHO function:

function getBlockCode_Echo( $iBlockID, $sContent ) {
return $sContent;
}

Notice the ECHO function does not have the ob_start(), eval() or the ob_get_clean() function.


HOW TO ADD A PHP BLOCK
-----------------------------------------------------------------------
To add a PHP block, you have to do the following:
1. Make an entry in the PageCompose table.
2. Add your PHP Code in the "Content" field of your new block entry.
3. In the Admin, you have to make sure it's active in the page.


STEP 1 : MAKE AN ENTRY IN THE PAGECOMPOSE TABLE

-----------------------------------------------------------------------
Login to your phpMyAdmin and run this SQL INSERT statement:

INSERT INTO `PageCompose` (`ID`, `Page`, `PageWidth`, `Desc`, `Caption`, `Column`, `Order`, `Func`, `Content`, `DesignBox`, `ColWidth`, `Visible`, `MinWidth`) VALUES ('', 'index', '960px', 'PHP BLOCK', 'PHP BLOCK', 2, 2, 'PHP', 'echo "You can now have PHP in a block!";', 1, 50, 'non,memb', 0)

It will make an entry in your table with the necessary field values. The most important value is the value that we add for the field Func. It has to be "PHP". Take note that a block function is identified in the database by what it appended. For example, the name of the function we are working with is "getBlockCode_PHP". Notice the "_PHP" in the function name. That tells Dolphin to look for the record in the PageCompose table that has "PHP" as the value in the Func field. If we created a custom function called getBlockCode_TEST, then we would need a corresponding Func value in the PageCompose table of "TEST". This is how the function matches up with the value that is found the database. In our example, it will spit out the value found in the Content field. That's where the PHP code would go.


STEP 2 : ADD YOUR PHP CODE
-----------------------------------------------------------------------
Once you've made the record entry in the PageCompose table, the rest is pretty easy. Just add you PHP code in the Content field and save. Go to step 3 to get a sample of a db connection and display your memebers.


STEP 3 : ADD THE PHP BLOCK VIA THE ADMIN
-----------------------------------------------------------------------
The SQL INSERT statement provided will automatically add the PHP Block in your admin under Builders so the only thing you would need to do is move the block to where you want it to be.

****One of the quirky things about the Dolphin is how it keeps and show information. In some cases you may need to refresh the page or move a block to make sure that your changes will update. For example, moving a block to another location in the page builder will usually do it.


Below is sample PHP code that will list all your members. You can copy and paste this bit of code in the PageCompose table.

$jt_dbHost="localhost";
$jt_dbLogin="yourlogin";
$jt_dbPass="yourpass";
$jt_db="yourdb";

$connection=mysql_connect("$jt_dbHost","$jt_dbLogin","$jt_dbPass") or die("Couldn't connect to the server.");

$db=mysql_select_db("$jt_db", $connection) or die("Couldn't select a database.");

$sql="SELECT * FROM Profiles";

$sql_result=mysql_query($sql,$connection) or die("Couldn't execute query!");

echo "PROFILES LISTING:<BR>";
while($row=mysql_fetch_array($sql_result)) {
$NickName=$row["NickName"];
echo "<br>$NickName";
}

echo "<br><br><br>CONGRATS! The db connected properly.";

mysql_close($connection);

Please try it out and let me know how it goes.

...sip...

Plussed by

 
 
 
 

Comments

Oldest First
|
Threaded
 
 
Please login to post a comment.
okweb
WORK GREAT
If you edit some code in it later, remember to move the block in admin to show your last changes on your website
jtadeo
Thanks okweb.

I have a few more advanced tutorials I am writing, if you're interested in reviewing them please let me know. Send me a PM etc and I will send you the links.
mscott
You sir are a genius, and the fact that only one person commented on this blog says a LOT about our community :-)
zankoku666
Wow this function just arrived on 6.1.2?

T_T my life is going to be easier I think Im gonna cry months and months on a learning curve with no end, thank you jtadeo...and the boonex dudes who realize that was necessary!

It worked perfectly, now Im wandering if I can include a file and then alot lots of things now can happen!!!
jtadeo
Hey guys thanks for the encouragement. I think it might be a bit too advanced for some or not too many people are interested. At any rate, I'm going to finish my basic tutorials and leave it at that.
buzzbot
Just so you know, you saved my butt :) I didn't know how I was going to pull this off until I came here :)

THANK YOU!
jonbrennan
Great job. In fact, you gave me the idea to try it with a 6.1.1 install. I added the function to the BxDolPageView and then added the block to the PageCompose per your tutorial which seemed very straight forward and well written. Worked without incident. Thanks!
bobp
I'm a brand new dolphin ..... so far in my very brief adventure, it seems that James provides more insight into the inner workings of Dolphin than I can find anywhere else.

James, thanks for this contribution!

I'm trying to unravel the dolphin template puzzle. If I could get a small assist with a few questions, I think I can be on my way.

For example, I'm trying to customize the Groups/Entry page (the page that displays when you click on a group that you belong to from 'my profile' page). see more I believe this involves grp.php in the top level folder, BxDolGroups.php in the inc/classes folder and page_71.html in the tmpl_uni folder.

Your example here, adding a PHP block is exactly what I need to add at the bottom of the group main display page. I understand how to create a PHP block, but how do I invoke the block from the group main display page?

In your example here, you talk about using the admin Pages Builder tool to place the PHP block on one of the pages. Which works great for pages that the tool handles. Unfortunately, the admin Pages Builder tool only manages a subset of pages including classifieds, homepage, account, shared music, shared photo, profile, and shared video.

The group display page is one of the pages that Pages Builder tool includes, so I believe I need to manipulate the appropriate template file and associated code that generates the content for the groups display page directly.

In my case, is there a way to add a reference to a custom PHP block (row in PageCompose table) in the page_71.html template? And would I need to add code to BxDolGroups.php to render? For example, in BxDolGroups, I think I would need to add code to the BxDolGroups class function 'GenGroupMainPage' that populates and returns an array of page element values?

Am I on the right track, or am I completely clueless? Can't seem to put it together.

Thanks in advance for your generous help.

Bob
bobp
I'm a brand new dolphin ..... so far in my very brief adventure, it seems that James provides more insight into the inner workings of Dolphin than I can find anywhere else.

James, thanks for this contribution!

I'm trying to unravel the dolphin template puzzle. If I could get a small assist with a few questions, I think I can be on my way.

For example, I'm trying to customize the Groups/Entry page (the page that displays when you click on a group that you belong to from 'my profile' page). see more I believe this involves grp.php in the top level folder, BxDolGroups.php in the inc/classes folder and page_71.html in the tmpl_uni folder.

Your example here, adding a PHP block is exactly what I need to add at the bottom of the group main display page. I understand how to create a PHP block, but how do I invoke the block from the group main display page?

In your example here, you talk about using the admin Pages Builder tool to place the PHP block on one of the pages. Which works great for pages that the tool handles. Unfortunately, the admin Pages Builder tool only manages a subset of pages including classifieds, homepage, account, shared music, shared photo, profile, and shared video.

The group display page is not one of the pages that Pages Builder tool includes, so I believe I need to manipulate the appropriate template file and associated code that generates the content for the groups display page directly.

In my case, is there a way to add a reference to a custom PHP block (row in PageCompose table) in the page_71.html template? And would I need to add code to BxDolGroups.php to render? For example, in BxDolGroups, I think I would need to add code to the BxDolGroups class function 'GenGroupMainPage' that populates and returns an array of page element values?

Am I on the right track, or am I completely clueless? Can't seem to put it together.

Thanks in advance for your generous help.

Bob
jtadeo
Hi bobp,

I will be updating this at my boonexnerd.com site. I am getting some help updating tutorial files etc.
bobp
yikes, sorry for the double post.

the second post has a correction - 'the group display page is not one of the pages that Pages Builder tool includes ....'

Bob
Varius
Hi,

I tried following your tutorial above, yet after doing the SQL INSERT statement and viewing it in the DB fine, nothing new appears in my Admin->Builer->Page Builder->Profile

Here is what i added to my db:

mysql> select * from PageCompose where ID=90 \G
*************************** 1. row ***************************
ID: 90
Page: profile
PageWidth: 317px
Desc: Profile Videos
Caption: Profile Videos
Column: 1
Order: 2
Func: PHP
Content: see more echo "Keith";
DesignBox: 1
ColWidth: 50
Visible: non,memb
MinWidth: 0

Any thoughts? using Dolphin 6.1.4
RumpyBumpy
Added PHP blocks and they show up in builder fine but finished page the block shows up with following inside:

Parse error: syntax error, unexpected '<' in /home/xxxxxx/public_html/dolphin/inc/classes/BxDolPageView.php(281) : eval()'d code on line 1

There is no < on line 281 in BxDolPageView.php .....
Where would I find the evaluated code to review line 1?

OR ..... what's wrong?
.
Snig
Same problem as RumpyBumpy. I added a ?> at the end of BxDolPage...php but I still get the same error.

Any ideas?
zak2008
First, I need to thank 'jtadeo' for posting his knowledge. It has worked most of it for me by following the steps given, but once I replaced the 'echo' statement in Content, I've been getting the same error as the others above:

Parse error: syntax error, unexpected '<' in /home/ethiomys/public_html/inc/classes/BxDolPageView.php(308) : eval()'d code on line 1

Anyone has a solution?
zak2008
After many trial and error, my block works fine. But it worked only because I had to remove all html and javascript codes inside the php script. I couldn't parse other scripts in the PHP Block unless its only php script.

Is there anyone with suggestion on how to get the PHP block working with php code that contains html or java codes?

Here is mine the couldn't work:

<?php
include "config.php";
include "functions.php";
?>

<script language="javascript" see more type="text/javascript">
if(top!=self)
{
top.location.href=self.location.href;
}
</script>

<div id="page">
<div id="header">
<div id="text">
<h1>Shoutbox</h1>
</div>
<div id="menu">
<a href="index.php<?php if($_SERVER['QUERY_STRING'] != ""){ echo "?" . $_SERVER['QUERY_STRING']; }?>">Refresh</a> | <a href="index.php?item=0">Shoutbox History</a> | <a href="http://code.google.com/p/freepss/">Get your own</a>
</div>
<div id="content">
<?php include"post.php"; ?>
</div>
</div>
<div id="footer">

<p class="copy">© 2006 All rights reserved.</p>
</div>
</div>
Snig
zak2008 had the answer. Once I removed the php tags from the mix the block works fine.
zak2008
Thx Snig...I forgot to post, but I did exactly what you sd, and indeed it works.
s4ns4n
I use dolphin 6.14
after reading your instructuion
i can't find PHP BLOCK in my BUILDER
what's wrong?
what version does this work with exactly? i have 6.1 so i dont want to create more problems yet.

thanks!
Endorfin
this is not really working, and it's so bad
melia
I tried this again and it's working fine. PHP blocks everywhere!
BoonexNerd_com
There's an update to this tutorial and new code will be released.
ygryggil
Is the new code released?
 
 
 
PET:0.071699857711792