Duplicate nodes returned from a module's SQL query I'm in a spot of bother customizing the calendar block module for my needs. I've written a custom module that queries my database for published nodes that possess a specific taxonomy term. This works as desired; 3 node objects are returned as an array. However, this array is returned a total of 35 times. When I execute the query in my SQL manager the 3 rows are returned without repetition. Is there anything blatantly amiss in my code I haven't been able to spot? 

/*
*
* Implementation of hook_calendar_block
*
*/

function calendar_block_mod_calendar_block(&amp;$calendar, &amp;$date, $op) {
  switch($op) {

   case 'alter':
                $sql = "SELECT DISTINCT node.nid AS nid 
                                FROM {node}
                                INNER JOIN term_node term_node ON node.vid = term_node.vid 
                                WHERE (node.status &lt;&gt; 0) AND (term_node.tid = 5)"; 

                $results = db_query($sql);

        while ($node = db_fetch_object($results))
        {    
          $events[] = node_load(array('nid' =&gt; $node-&gt;nid));
        }

        foreach ($events as $var){
                $thisPath = $var-&gt;path;
                                $nodeTitle = $var-&gt;title;
                $thisDate = substr($var-&gt;field_event_date[0]['value'],0,10);
                                $realDay = substr($thisDate, 8,2);
                $realDate = substr($thisDate, 5, 2) . "-" . ( $realDay &lt; 10 ? substr($realDay, 1,1) : $realDay ) ."-". substr($thisDate, 0, 4);

                                //$dayInfo[] = array ('date' =&gt; $realDate, 'title' =&gt; $nodeTitle,  'path' =&gt; $thisPath );

                                if ($date-&gt;date == $realDate) {

                                    $date-&gt;content = '&lt;span class="tooltip"&gt;'. l($date-&gt;day, $thisPath) .'&lt;/span&gt;';
                                    $date-&gt;content .= '&lt;div class="tooltip_hidden"&gt;'. l($nodeTitle, $thisPath) .'&lt;/div&gt;';

                                }
                            }
          break;

        case 'load':

        // Change the weekday's format and set the first day of the week to sunday.
        $calendar-&gt;weekdays =  array(
            'su' =&gt; 'S',
            'mo' =&gt; 'M',
            'tu' =&gt; 'T',
            'we' =&gt; 'W',
            'th' =&gt; 'T',
            'fr' =&gt; 'F',
            'sa' =&gt; 'S',
        );

        break;
  }

}


Edited to include all the code from my module - If anything I hope this can give others a similar starting point. if the code looks like a mess it's because it's my first module.

The code works: nodes with the desired taxonomy term are pulled from the database and each date on the calendar becomes a link to its corresponding node. However on closer inspection the 3 rows are returned 35 times, so doing anything beyond this (e.g. a choice of nodes if there are multiple ones on a single date) becomes a challenge / impossible.
I just took a quick look at the calendar_block module. From what I can tell, hook_calendar_block op alter is called for every single day in the calendar. I guess this makes sense at it allows you to alter the appearance of any day.

My guess is that the calendar on your site has 35 days in it which is why your query is run 35 times.

You could make use of a static variable to only do your stuff once or save the query etc.

Something like this could work:

function calendar_block_mod_calendar_block(&amp;$calendar, &amp;$date, $op) {
  switch($op) {
    case 'alter':
      static $events;
      if (empty($events)) {
        $sql = "SELECT DISTINCT node.nid AS nid 
              FROM {node}
              INNER JOIN term_node term_node ON node.vid = term_node.vid 
              WHERE (node.status &lt;&gt; 0) AND (term_node.tid = 5)"; 

        $results = db_query($sql);

        while ($node = db_fetch_object($results)) {    
          $events[] = node_load(array('nid' =&gt; $node-&gt;nid));
        }
      }
      ....
  }
}


The above code doesn't catch the case where there is no result and is just meant as a proof of concept.

Update:
If you only want to run the entire code once, you could do something like this:

function calendar_block_mod_calendar_block(&amp;$calendar, &amp;$date, $op) {
  switch($op) {
    case 'alter':
      static $code_executed;
      if (!$code_executed) {
        ...
        $code_executed = TRUE;
      }
  }
}