I'm trying to make a page that displays the most recent node's teaser followed by a node_title_list that excludes the most recent node.

I've already set up a page that displays the teaser for the most recent node and displays a node_title_list, but I can't figure out how to exclude the node whose teaser is displayed from the node_title_list:

<?php $nlimit = 1; ?>
<?php $result1 = db_query("SELECT n.nid, n.created FROM {node} n WHERE n.status = 1 AND n.type = 'flexinode-2' OR n.type = 'flexinode-7' OR n.type = 'flexinode-6'  OR n.type = 'flexinode-11' OR n.type = 'flexinode-4' ORDER BY n.created DESC LIMIT $nlimit"); ?>
<?php while ($node = db_fetch_object($result1)) {$output .= node_view(node_load(array('nid' => $node->nid)), 1);}; ?>
<?php print $output; ?>


<?php $n2limit = 25; ?>
<?php $result2 = db_query("SELECT n2.created, n2.title, n2.nid, n2.changed FROM drupal_node n2 WHERE n2.status = 1 AND n2.type = 'flexinode-2' OR n2.type = 'flexinode-7' OR n2.type = 'flexinode-6'  OR n2.type = 'flexinode-11' OR n2.type = 'flexinode-4' ORDER BY n2.changed DESC LIMIT $n2limit"); ?>
<?php $output2 .= "<div class=\"item-list\"><ul>\n"; ?>
<?php $output2 .= node_title_list($result2); ?>
<?php $output2 .= "</ul></div>"; ?>
<?php print $output2; ?>

One method I thought might work: save the first node's nid and exclude it from the db_query. The problem is I don't know how to include variables in the query. I tried saving the first node's nid to a variable $first, but it just causes a mySQL error when I place it in the db_query, as in "AND n2.nid != $first" or "AND n2.nid != $result1->nid" or something similar.

Any thoughts?

Forrest

Comments

nevets’s picture

Here is the code modified to track the nid(s) of the content viewed so it does not also appear in list of nodes.
I also changed the query logic slightly to remove all the ORs. It also removes all the extra php open/close syntax.

<?php
$nlimit = 1
$result1 = db_query("SELECT n.nid, n.created FROM {node} n WHERE n.status = 1 AND n.type IN ('flexinode-2', 'flexinode-7', 'flexinode-6', 'flexinode-11', 'flexinode-4') ORDER BY n.created DESC LIMIT $nlimit");

$nids = array();
while ($node = db_fetch_object($result1)) {
  $output .= node_view(node_load(array('nid' => $node->nid)), 1);
  $nids[] = $node->nid;
}
$exclude = implode(',', $nids);
print $output;
$n2limit = 25;
$result2 = db_query("SELECT n2.created, n2.title, n2.nid, n2.changed FROM drupal_node n2 WHERE n2.status = 1 AND n2.type IN ('flexinode-2', 'flexinode-7', 'flexinode-6', 'flexinode-11', 'flexinode-4') AND n.nid NOT IN (%s) ORDER BY n2.changed DESC LIMIT $n2limit", $exclude);
$output2 .= "<div class=\"item-list\"><ul>\n";
$output2 .= node_title_list($result2);
$output2 .= "</ul></div>";
print $output2;
?> 

Note this version assumes you view at least one node.
Also, if you used one of n.created in n.changed in both queries you could simplify it something like this. This version uses n.created. It also removes all the extra php open/close syntax.

<?php 
$nlimit = 1;
$sql = '"SELECT n2.created, n2.title, n2.nid, n2.changed FROM drupal_node n2 WHERE n2.status = 1 AND n2.type IN ('flexinode-2', 'flexinode-7', 'flexinode-6', 'flexinode-11', 'flexinode-4') AND n.nid NOT IN (%s) ORDER BY n2.changed DESC";
$result1 = db_query_range($sql, 0, $nlimit);
while ($node = db_fetch_object($result1)) {
  $output .= node_view(node_load(array('nid' => $node->nid)), 1);
}
print $output;
$n2limit = 25;
$result2 = db_query_range($sql, $nlimit, $n2limit);
$output2 .= "<div class=\"item-list\"><ul>\n";
$output2 .= node_title_list($result2);
$output2 .= "</ul></div>";
$output2;
?> 
forrestn’s picture

Amazing!

Thanks so much for this wonderful reply! You truly are an expert at this stuff. Wow!

I used the first code, and it worked perfectly (except I had to add a semicolon after $nlimit = 1, and change "n.nid" to "n2.nid" in the second SQL query, no big deal). Thanks so much!

You should consider submitting this to the documentation somewhere, it was very helpful to me.

Forrest