To create a block of the third type, i.e. a php script that interfaces with the database and extrapolates the data, first of all we have to understand how these blocks are structured. They are contained in a folder called blocks, the name of the block must be block-blockname.php. It is very important all blocks start with "block-" . Every block in which the name will start with block- will be included in the screen of the blocks that can be activated. We will find in the blocks administration menu all the available blocks in the file_name drop-down list. If not assigned by the administrator, the name will be the same that follows block- We can't use break spaces in a block name, they must be replaced by using underscore _ . All the blocks that will respect these rules will be inserted in the blocks admin menu.
How to create a block, theoretical approach:
You have to follow these rules when creating a block:
In every block you create you have to insert the following code at the beginning:
if (eregi("block-Name_of_Block.php", $PHP_SELF)) {
Header("Location: index.php");
die();
}
By using this code you protect the file avoiding users approaching it directly from the blocks folder, and the block will be displayed only when selected from your site.
In the blocks you can include everything you want, Perl, java, php, flash etc...
All the block output must have a value that can be obtained from the variable $content.
Remember that you have a limited amount of space in the block, pay special attention to the layout!
Warning |
In order to have W3C standard compatible blocks, Francisco Burzi says:
"To respect the W3c standards for HTML 4.01 Transitional, it is very important that you replace all "instances of &" in the URL by the tag "& " For example the URL:
<a href="modules.php?op=modload&name=FAQ&file=index"> must be written:
<a href="modules.php?op=modload&name=FAQ&file=index"> and don't use, for example, the tag "li" (used to create a list), but leave that in the style sheet (CSS) which will make it for you. |
The background for the tables and font etc., are better left to the style sheet (CSS).
We will now see how to construct a block starting from the beginning.
How to create a block, a practical example
We will make a very simple block that shows the pages visited in our site the day before. We'll have a single query and a single value, in order to make things easier. Our block is called "hits", so the complete name of the block will be block-hits.php
First of all, we open the php tag
<?php
Then we insert the protection script we've seen before:
if (eregi("block-hits.php", $PHP_SELF)) {
Header("Location: index.php");
die();
}
And now we insert the variables that we want to call (in this case the parameter $prefix and $dbi, which handles the database abstraction):
global $prefix, $dbi;
Now we continue inserting the query that reads from the database how many pages were seen in our site: (the instruction would be "read the first line value of the table nuke_counter in the cell count")
$result = sql_query("select count from "$prefix."_counter order by type desc limit 0.1", $dbi);
list($count) = sql_fetch_row($result, $dbi);
Finally, we pass the "$content" variable that will be echoed by the block and close the PHP tag:
$content. = $count
?>
Our complete script will be :
<?php
if (eregi("block-hits,php", $PHP_SELF)) {
Header("Location: index.php");
die();
}
global $prefix, $dbi;
$result = sql_query("select count from "$prefix."_counter order by type desc limit 0.1", $dbi);
list($count) = sql_fetch_row($result, $dbi);
$content. = $count
?>