Counting articles

3 posts by 2 authors in: Forums > CMS Builder
Last Post: February 19, 2009   (RSS)

By flamerz - February 19, 2009

I searched info about this one in forum with no luck.

I have an articles archive like this:

Archive
2009 ......... 55 articles
2008 ......... 315 articles
2007 ......... 155 articles

There is a way to get this kind of article count by year?

thanks in advance.

Re: [flamerz] Counting articles

By Dave - February 19, 2009

Hi Flamerz, what you have works just fine.

There's a new function that will be released in 1.27 that gets the count for you. If you want to use it now you can add it yourself to cmsAdmin/lib/database_functions.php. Here's the code:

function mysql_select_count_from($tableName, $whereClause = '') {
if (!$tableName) { die(__FUNCTION__ . ": No tableName specified!"); }

$tableNameWithPrefix = getTableNameWithPrefix($tableName);
$escapedTableName = mysql_real_escape_string( $tableNameWithPrefix );
if ($whereClause != '') { $whereClause = "WHERE $whereClause"; }

$query = "SELECT COUNT(*) FROM $escapedTableName $whereClause";
$result = @mysql_query($query) or die(__FUNCTION__ . "() MySQL Error: ". htmlspecialchars(mysql_error()) . "\n");
list($recordCount) = mysql_fetch_row($result);
if (is_resource($result)) { mysql_free_result($result); }

//
return $recordCount;
}


Once that's added you can get a count like this:

<?php echo mysql_select_count_from('news', 'YEAR(`date`) = 2009'); ?>

But if what you have now is working you'd be fine just to stick with that as well.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com