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
if $output return TRUE
No, because $output is not accessible AFAIK in the visibility box--it is defined in the content of the block. You would have to write the logic code again in the visibility block, but just the logic to determine if there is or isn't output. And don't forget that you are checking not only that there is output, but also that the path is one of blog/*, video/*, image/*, etc.
HTH
Do one thing,
fine max rows, when greater then 0 means, display the block...
On Thu, Jun 26, 2008 at 9:48 PM, Fred Jones fredthejonester@gmail.com wrote:
if $output return TRUE
No, because $output is not accessible AFAIK in the visibility box--it is defined in the content of the block. You would have to write the logic code again in the visibility block, but just the logic to determine if there is or isn't output. And don't forget that you are checking not only that there is output, but also that the path is one of blog/*, video/*, image/*, etc.
HTH
[ Drupal support list | http://lists.drupal.org/ ]
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@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/ ]