[support] how can I stop a block showing if it's empty? (not a Views block)

Neil: esl-lounge.com neil at esl-lounge.com
Thu Jun 26 15:45:07 UTC 2008


I have blocks called "Related Blogs" or "Related Videos" etc, which list other nodes of the same type if they've been tagged with the same terms. It's working great, just that the block is showing even when there are no related nodes. What I would like is for the block not to show if there are no related nodes.

My code is:

<div style="font-size:0.75em;padding:3px;">
<!-- 4 returned for each term, ordered by nid descending, meaning new will always be at top -->
<?php
if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
  $nid = (int)arg(1);
  $terms = taxonomy_node_get_terms($nid);
  $output = "<span class='whole-list'>";
  foreach($terms as $term){
    $sql = "SELECT n.title, n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid = $term->tid AND n.nid != $nid ORDER BY RAND() LIMIT 3";  
    $result = db_query(db_rewrite_sql($sql));
    if (db_num_rows($result)) {
      $output .="<h5 style='color:#900;font-weight:bold;margin-top:3px;'>$term->name</h5><ol class='each-term'>";
      while ($anode = db_fetch_object($result)) {
        $output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
      }  
      $output.="</ol>";
    }
  }
  $output .= "</div>";
  return $output;
}
?>
</div>

and for the moment, the block visibility is set as blog/*, video/*, image/*, etc. How can I use php visibility settings to test to see if any content is returned. Would something as simple as:

if $output
return TRUE

work?

Neil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20080626/2fa4c969/attachment.htm 


More information about the support mailing list