Off by One Error

callison - December 4, 2008 - 02:28

I'm working on a page that will display all nodes and their attached files from a given taxonomy term. For some reason, though, I am getting a weird error where it won't display the latest node. As far as I can tell, everything looks good with the code. Would somebody please review my code and tell me if you see something I'm missing. Thanks in advance.

- Chris -

<?php
// comma separated lists of terms tid to display nodes
$terms = "6";

$sql = "SELECT n.nid
  FROM {node} n
  INNER JOIN {term_node} tn ON n.nid = tn.nid
  WHERE tn.tid IN ($terms) AND n.status = 1
  ORDER BY n.created DESC"
;

$result = db_query(db_rewrite_sql($sql));

if (
db_result($result)) {
  while (
$data = db_fetch_object($result)) {
   
$node = node_load($data->nid);
   
$file_link = "No File Available";
    foreach(
$node->files as $item ) {
     
$file_link = l($item->filename, file_create_url($item->filepath));
    }
   
$rows[] = array(l($node->title, $GLOBALS['base_url'] . "/node/" . $node->nid), $file_link);
  }
}

$header = array(
  array(
'data' => t('Page')),
  array(
'data' => t('PDF')));

print
theme('table', $header, $rows);

?>

Fixed it

callison - December 15, 2008 - 15:38

Just in case anybody stumbles upon this with a similar problem, I figured out (doh!) that all I had to do was replace
if (db_result($result)) {
with
if ($result) {

db_result() advances to the next item, so when you check it, it returns true, but you skip the first item.
www.allisonmission.com

 
 

Drupal is a registered trademark of Dries Buytaert.