|
Blocks are a great way to code a little application for indexU without having to get swamped in code from other core files. In this block tutorial i will show you how to write a block for the detail page. This block will flow as follows :
1) Block is called from the detail_link.html file 2) The Block passes the variable "link_id" to the php file, this allows us to know which link we are dealing with 3) The php file collects the Link_id as a "parameter" does a query to the database using it. 4) We assign the result to a template and display it.
The block is going to be called "hit counter", so lets start with the easy bit and place the following code in detail_link.html
<%block_hitcounter%>
This will now cause an error, we have 2 problems with the above. Firstly it does not pass the link_id, the second problem is that its going to look for a file called "hitcounter.php" which currently does not exist. Lets fix these problems. Firstly lets pass the link Id to it .
<%block_hitcounter id="$link_id"%>
my changes, in Kosmos theme, now looks like this : <tr class="tbl_normal"> <td>Url</td> <td><a href="<%$url%>" name="link_<%$link_id%>"><%$url%></a> ( hits : <%block_hitcounter id="$link_id"%> )</td> </tr> Now to create the block, paste the following into a new document and save it down to blocks/block.hitcounter.php <?php function block_hitcounter($params) { global $dbConn, $id; if (!$params['template']) { $params['template'] = 'block.hitcounter.html'; } //here i assign the parameter passed to the php file to a variable called "id" $id= $params['id'];
$tpl = new Template;
// here i do a query ont he database $query = "select hits as h from idx_link where link_id=$id"; $result = $dbConn->Execute($query); // assign the resultign field to a variable called hits $hits = $result->Fields("h");
// assign the variable to the template. $tpl->assign("hits", $hits); $tpl->display("blocks/" . $params['template']); }
?>
read the comments to figure out what is happening in the file, if your creating your own block then you can use the above example as a basis. It demonstrates how you pass a value to the php file, execute some code, the put the result back to a template. Now all we need to do is create the html file, we dont need any layout on the template file so create a new document, save it as themes/blocks/block.hitcounter.html All you need in this file is : <%$hits%> Now upload everything and try it out! This is a very basic example, I will cover how to work with loops in a subsequent article. Things to watch out for The function name at the top (function block_hitcounter($params) { ) must start with block_ and then be the exact filename otherwise it will not execute. The code used to callt he block must always be <%block_hitcounter%> (note you dont use the "$" like you would with a custom field as this is not a variable. Lastly, make sure that any variables you pass to the php file are enclosed in quotes "like this"
|