How to list the latest 11-20 nodes in Drupal (2)

According to the question and my snippets, the same one asked me again to extend the previous codes to show the author's name besides the node title. In addition, it would be nice to display node title and its author in a table form. Fortunately, this is Drupal. It is very easy.

In order to obtain the autor's name of each node, I just simply joined node to users using uid. You may use user_load() but it might be pretty slow. However, node_title_list() is not applicable here. As a result, I use l() instead.


$str_tids = "22";
$order = "n.created DESC";
 
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created, u.uid, u.name FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1';
$result = db_query_range(db_rewrite_sql($sql .' ORDER BY '.$order), 10, 10);
if (db_num_rows($result)) {
  $items = array();
  while ($node = db_fetch_object($result)) {
    $items[] = l($node->title, 'node/'. $node->nid) .' ('. l($node->name, 'user/'. $node->uid) .')';
  }
 
  echo theme('node_list', $items);
}
?>

To render a table, I simply used theme_table().


$str_tids = "22";
$order = "n.created DESC";
 
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created, u.uid, u.name FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1';
$result = db_query_range(db_rewrite_sql($sql .' ORDER BY '.$order), 10, 10);
if (db_num_rows($result)) {
  $header = array('Title', 'Author');
  $rows = array();
  while ($node = db_fetch_object($result)) {
    $rows[] = array(
      array('data' => l($node->title, 'node/'. $node->nid), 'class' => 'title'),
      array('data' => l($node->name, 'user/'. $node->uid), 'class' => 'author'));
  }
 
  echo theme('table', $header, $rows);
}
?>

Tags: ,

Reply