Community Documentation

List node count for each node type

Last updated March 12, 2008. Created by JuliaKM on March 12, 2008.
Log in to edit this page.

This snippet is will return a table with each node type and the number of nodes published for each type over the past three years.

<?php
function custom_count_pull(){
   
$sql = "SELECT type, COUNT(nid) AS number, FROM_UNIXTIME(created, '%Y')  AS createdate FROM {node} GROUP BY type, createdate ORDER BY createdate DESC";
   
$result = db_query($sql);
   
$article_count = array();

    while (
$data = db_fetch_object($result)){
        if (isset(
$article_count[$data->type])) {
       
           
$article_count[$data->type][$data->createdate] = $data->number;
        } else {
           
$article_count[$data->type] = array($data->createdate => $data->number);
        }
    }
   
$today = date("Y");
   
$years = array($today-2,$today-1,$today);

   
$output .= "<table><tr><th>Type</th><th>{$years[0]}</th><th>{$years[1]}</th><th>{$years[2]}</th></tr>";   
    foreach (
$article_count as $type => $value) {
       
$real_type_name = node_get_types('name', $type);
       
$output .= "<tr> <td>$real_type_name</td>";

        foreach (
$years as $year) {
            if (
$value[$year]):
           
$output .= "<td>{$value[$year]}</td>";
            else:
           
$output .= "<td>0</td>";
            endif;
        }
       
$output .= "</tr>";
    }
   
$output .= "</table>";
    return
$output;
}
?>

To make the table display, you will need to also include:

<?php
print custom_count_pull();
?>

Comments

Any modules available?

There is:
http://drupal.org/project/term_node_count
But doesn't work this way.

About this page

Drupal version
Drupal 5.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here