Last updated June 4, 2005. Created by laura s on June 3, 2005.
Edited by Dublin Drupaller. Log in to edit this page.

If you are using two or more php snippets in the same node - or on your front_page.module page - add the following line at the beginning of each php snippet you intend to use

<?php
unset ($output);
?>

why?

The reason is to avoid the problem of using the return $output; more than once in the same node. The return $output command will end the reading of code and all subsequent code or text in that node (or front_page) will be ignored.

To avoid potential problems, rather than return $output;, you can use...

<?php
print $output;
?>
...for the last line of code.

But...

With each subsequent php-generated list you create in that same node (or front_page), you will get a cumulative list -- the content generated by each additional php snippet will be added to the $output list. (For example, the second php snippet's list of posts on "apples" would be added to the first php snippet's list of posts on "oranges," when what you really want is just a list of posts on "apples.")

So how would one "clear" the output results so one can have another php snippet creating output? Start each of your subsequent snippets of php code with this:

<?php
unset ($output);
?>

This will clear the previous results so that your 2nd, 3rd ... nth list will have unique results relevant to its own query.

Thus you will be able to have several php-generated outputs on the same page (such as lists of recent posts in several different taxonomy terms) without conflict or intermingling of results.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

I'm using the following snippet 3 times on a custom front_page. This is the third of the 3, and contains the unset ($output); line. This is a 5.1 site and I'm still getting cumulative results. Any help is greatly appreciated.

<?php
unset ($output);
// tag to use:
$tag = 'Article';
// maximum number of items to show:
$count = 6;
$result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title
FROM {node} n
INNER JOIN {term_node} tn ON n.nid = tn.nid
INNER JOIN {term_data} td ON tn.tid = td.tid
WHERE td.name = '%s' AND
n.status = 1
ORDER BY n.created DESC"
),$tag,0,$count);
while (
$node = db_fetch_object($result)) {
 
$items[] = l($node->title, 'node/'. $node->nid);
}
$output = theme('item_list', $items);
print
$output;
?>

Lanexa.net
@lanexa

nevets posted the solution here - http://drupal.org/node/158353#comment-250567

<?php
unset ($output);
unset (
$items);
?>

Lanexa.net
@lanexa