How to create a block, a practical example

20.2.2. 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
?>
 

Site Info v2.2.2

Last SeenLast Seen
Server TrafficServer Traffic
  • Total: 343,977,755
  • Today: 35,260
Server InfoServer Info
  • Dec 13, 2017
  • 02:50 pm PST
 
 

Daily Inspiration