Question

I have a Joomla site that I am developing and I am a novice when it comes to MySql. I need to be able to track how many articles a user has viewed and then be able to display the total number of viewed articles on the front end of the page (for instance if the user has viewed 3 articles, it should just echo the value '3' to the front end).

I'd also like to be able to store the article id and alias and be able to retrieve that data later if needed. The idea is that any particular user will be able to see how many articles they have viewed and possibly even be able to go back and review those articles (as they will no longer be displayed once viewed).

I haven't the faintest idea of where to start as far as building the table to store this data, and how to join it to the current user. Any help would be greatly appreciated, even if it is just pointing me to a good tutorial or resource.

Was it helpful?

Solution

There are three approaches, I believe:

Store All Viewed Article IDs in New Column In User Table
You could add a column to your user table to store the viewed articles. You could store the article ids as comma-separated values. Each time a new article is viewed you can append a new article ID. On the front-end, you can retrieve this string with a select statement and parse it out to get the individual IDs.

This method may not be practical with a CMS such as Joomla as adding to columns to system tables is asking for trouble.

New Table With One Row Per User
You create a new two-column table, one column has the user id and the other has the comma-separated list of viewed articles. There would be a 1-0 relationship between the existing user table and the new table.

A simple join will allow you to retrieve the viewed articles associated with the associated user.

New Table With One Row Per User/Article Combination
You create a new two-column history table which stores the user and a single viewed article ID. If a user has viewed ten articles, there will be ten rows.

Retrieving the viewed articles for a given user takes some work as you have to pull in all rows for that user and iterate through them.

Edit - Sample Code:

<?php
$config['db'] = array (
    'host' => '',
    'username' => '',
    'password' => '',
    'dbname'    => ''
);

$db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname='.$config['db']['dbname'],$config['db']['username'],$config['db']['password']);

$query = $db->query("SELECT user_id, article_id FROM viewed_articles WHERE user_id = 1234");

$rows = $query->fetchAll(PDO::FETCH_ASSOC);

echo '<pre>',print_r($rows,true),'</pre>';

?>

You would put in your MySQL DB and credentials in the $config array.

OTHER TIPS

A good approach is to use a module to show the data and a plugin to store it.

To track the articles viewed by an user start with the Joomla documentation for plugin development. and look for content plugins. This plugin should store, at least, the user identifier and the article identifier on a table (call it articleviews) created specially for this plugin.

To build a module check the Joomla module development. On this module you will load the data stored with the plugin from the articleviews table. Remember to index the table by user identifier, otherwise it will kill your web response as data grows.

EDIT: article identifier should be part of the index too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top