Let's take a look at a block's structure to maybe understand how to create one yourself, take into consideration that if you want to create a simple html block that won't be making any calls to the database you can simply follow the instructions described in the Blocks in PHP-Nuke section, no need to place that code in a file to be able to use it. The concept behind creating a file block is to be able to have it interact with the database and that it is able to grab the variables available in PHP-Nuke's core structure, now let's use C. Verhoef's Total hits block as an example in order to attempt to explain how each area's works:
<?php
######################################################################## if (eregi("block-Total_Hits.php", $PHP_SELF)) { global $nukeurl, $prefix, $startdate, $dbi; $result = sql_query("SELECT count FROM ".$prefix."_counter
WHERE type='total' AND var='hits'", $dbi); ?> |
Let's dissect the block in sections to explain each:
<?php (like any other php file our block should start with this tag at the very first line) |
if (eregi("block-Total_Hits.php", $PHP_SELF)) { This section protects your block from being accessed directly in case someone tries to type the path in the url (http://yourwebsite/blocks/block-your_block.php will not work thanks to this code). |
global $nukeurl, $prefix, $startdate, $dbi; If you were to use any of PHP-Nuke's global variables you'd need to list the desired variables in a line similar to the one above, this will pass said variables into your code. |
$result = sql_query("SELECT count FROM ".$prefix."_counter
WHERE type='total' AND var='hits'", $dbi); This would be the actual code, the first two lines query the database to get the information to be used by your block, these lines do not get displayed in the actual block because as you'll notice they start with $result which in simple terms is a variable that will get defined by the data it pulls from the database, in this case it attempts to get a number from the counter database table, so when the query is resolved $result will become the number it finds in that table. What is displayed in the block is defined in the $content variable, this variable is made up of simple html code which now has the $result variable we described above, the words that start with an underscore and are all uppercase letters are actually language constants that get defined in PHP-Nuke's language files, these constants will often help you save some space in the code as you could take an entire paragraph that you intend to use more than once and insert it into a language file, if for example i were to use the line in the table above this one a lot i could add a constant to the language file in the following format: define("_GLOVAR","If you were to use any of PHP-Nuke's global variables you'd need to list the desired variables in a line similar to the one above, this will pass said variables into your code"); Now i'd be able to insert that line anywhere in a file by using "._GLOVAR." instead of having to type the entire line. Now to further explain the format of the $content variable, like i mentioned it can use normal html but there are some rules that need to be observed, for instance when inserting hyperlinks into a php file you must add backslashes to any double quotes that may be enclosed between html tags, this said, the above code has a hyperlink but instead of using a normal <a href="modules.php?name=Statistics"> you need to make it: <a href=\"modules.php?name=Statistics\">. If your html code had more than one line you'd have to extend the content variable in the following format: $content = "<your first html line here>"; One other thing to consider is PHP-Nuke's use of W3c standards for HTML 4.01 Transitional, when using hyperlinks in a php block you need to make at least one other change in the url structure, PHP-Nuke uses the ampersand symbol in many of its sections, to have your code remain compliant to html 4.01 Transitional you need to change all instances of the ampersand symbol (&) to & |
?> (Your php code should be closed with this tag) |
Page Submitted byChatServ