Jamroom Module Guide - get_counts.php
If your module stores items in the Jamroom Database, and you would like to have the "number" of items in your custom database table be available through the Jamroom {jr_counts} template function, then you can define a special "get_counts.php" script in your module directory that provides a way to get the count requested.
For an overview of how the {jr_counts} template function is used, make sure and check out the documentation page.
Here's an example script that will retrieve the number of YouTube entries
<?php
/**
* Jamroom jrYouTube Get Counts template plugin file
* @copyright 2009 by Talldude Networks LLC.
* @author Brian Johnson - bigguy@jamroom.net
*/
// make sure we are not being called directly
defined('IN_JAMROOM') or exit();
/**
* Creates the proper function for getCounts()
* @param array Parameters passed in to the {jr_counts} template function
* @return int Returns a number
*/
function jrYouTube_get_counts_youtube_videos(&$_args)
{
global $jamroom_db;
// If we are not given a band_id, it is much
// faster to grab the total number of db rows
if (!checkType($_args['band_id'],'number_nz')) {
$num = dbNumberRows($jamroom_db['jrYouTubeVideos']);
}
else {
$req = "SELECT COUNT(youtube_id) AS vcount
FROM {$jamroom_db['jrYouTubeVideos']}
WHERE youtube_band_id = '{$_args['band_id']}'
GROUP BY youtube_id";
$_rt = dbQuery($req,'SINGLE');
$num = (int) $_rt['vcount'];
}
return $num;
}
?>
You can see that this script is actually a very simple script. The purpose of the single function is to return the the count of database rows as requested. Starting from the top we see:
<?php
/**
* Jamroom jrYouTube Get Counts template plugin file
* @copyright 2009 by Talldude Networks LLC.
* @author Brian Johnson - bigguy@jamroom.net
*/
// make sure we are not being called directly
defined('IN_JAMROOM') or exit();
Like our other module PHP scripts, it is important that we do our "IN_JAMROOM" check - this prevents the PHP script from being executed outside of Jamroom.
Next up is the actual function that returns the count of database rows. Like other functions within your module, the NAME of the function is very important - if it is not named correctly, Jamroom will not see it. For functions defined in the get_counts.php script, they must adhere to the following naming format:
MODULENAME_get_counts_JRCOUNTSTYPE
so our example function is named "jrYouTube_get_counts_youtube_videos". This will allow a user to get the counts from the jrYouTube module database like so:
{jr_counts type="youtube_videos" formatted="true"}
/**
* Creates the proper function for getCounts()
* @param array Parameters passed in to the {jr_counts} template function
* @return int Returns a number
*/
function jrYouTube_get_counts_youtube_videos(&$_args)
{
global $jamroom_db;
// If we are not given a band_id, it is much
// faster to grab the total number of db rows
if (!checkType($_args['band_id'],'number_nz')) {
$num = dbNumberRows($jamroom_db['jrYouTubeVideos']);
}
else {
$req = "SELECT COUNT(youtube_id) AS vcount
FROM {$jamroom_db['jrYouTubeVideos']}
WHERE youtube_band_id = '{$_args['band_id']}'
GROUP BY youtube_id";
$_rt = dbQuery($req,'SINGLE');
$num = (int) $_rt['vcount'];
}
return $num;
}
?>
As many different functions as you would like can be added in to this file - each one will define a different "type" for the {jr_counts} function.
|