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

Kathleen Murtagh kathleen at ceardach.com
Thu Jun 26 18:20:31 UTC 2008


A block won't display if there is no content inside of it to display.
Rework your code so no HTML is printed when there are no results to
show.  For example:

<?php
  if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
    $nid = (int)arg(1);
    $terms = taxonomy_node_get_terms($nid);
    foreach($terms as $term){
      // 4 returned for each term, ordered by nid descending, meaning
new will always be at top
      $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>";
      }
    }
  }

  if($output) {
    print '<div style="font-size:0.75em;padding:3px;">';
    print '<span class="whole-list">';
    print $output;
    print '</span>';
    print '</div>';
  }
?>

---
Kathleen Murtagh


On Thu, Jun 26, 2008 at 11:45 AM, Neil: esl-lounge.com
<neil at esl-lounge.com> wrote:
> 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
> --
> [ Drupal support list | http://lists.drupal.org/ ]
>


More information about the support mailing list