27 July 2009 View Comments

Recording traffic using Joomla!


今天介绍如何使用Joomla来记录浏览量。
在这儿的浏览量简单的定义为用户通过url进入某个页面的次数,包括刷新页面。

Summary&pre-requirement

The key point of this example is to use the hit() member function of JTable.

I assume you have a local Joomla! 1.5 development set up and running.

I assume you have a table called jos_foo, like jos_foo

Field Type Allow Null Default Value
id int(11) No
name varchar(255) Yes
favorite_food varchar(255) Yes
bio text Yes
checked_out tinyint(1) Yes
checked_out_time datetime Yes
editor varchar(255) Yes
hits int(11) UNSIGNED Yes 0
access int(11) UNSIGNED Yes
groupname varchar(255) Yes
ordering int(11) Yes

Step 1:

Because we created a table in the database, we will need to access it, this can be done by using a foo.php program in components/com_foo/tables/ , it is as follows:

[cc lang="php" line_numbers="true"]
defined( ‘_JEXEC’ ) or die( ‘Restricted access’ );

class TableFoo extends JTable
{
var $id = null;
var $name = null;
var $favorite_food = null;
var $bio = null;
var $checked_out = null;
var $checked_out_time = null;
var $editor = null;
var $hits = null;
var $access = null;
var $groupname = null;
var $ordering = null;

function __construct( &$db )
{
parent::__construct( ‘#__foo’, ‘id’, $db );
}
}
[/cc]

Step 2:

This is the man program we’re creating now. We will first need to create a folder within components as com_foo, and a file called foo.php, this file can be accessed by going to /components/com_foo/foo.php

We write the following code in it.

[cc lang="php" line_numbers="true"] <?php
defined (’_JEXEC’) or die (’Restricted access’);
jimport(’joomla.application.component.controller’);
JTable::addIncludePath(JPATH_COMPONENT . DS . ‘tables’);
class FooController extends JController {

function showFoo() {
$id = JRequest::getInt(’id’, 0);
$row =& JTable::getInstance(’Foo’, ‘Table’);

$row->load($id);
$row->hit($id);

?>
<p><strong><?php echo $row->name; ?></strong></p>
<p>Favorite food: <?php echo $row->favorite_food; ?></p>
<p><?php echo $row->bio; ?></p>
<?php
}
}
$controller = new FooController();
$controller->execute(JRequest::getCmd(’task’));
[/cc]

Step 3:

Let’s see if we can access:

http://your_local_joomla_development_address/index.php?option=com_foo&task=showFoo&id=2

And as you can see the foo.php is working

Fresh and Local.png

We then check what happen we refresh or load this page again. We first record the database shown below. After we refreshed the page, we will see the hits goes up by one, which shows the code is working.

[Table View] jos_foo @joomla (Local).png
blog comments powered by Disqus