Counting articles

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

Re: [flamerz] Counting articles

By flamerz - February 19, 2009 - edited: February 19, 2009

i reply myself.

If somebody has more efficient query (p.e. grouping), feel free to fix me. (im new here)

<?php
list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'where' => "YEAR(`date`) = 2009",
'allowSearch' => '0',
));
?>

<?php echo count($newsRecords) ?>

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