I found this post here:
http://drupal.org/node/553396#comment-2263590

which gave the code below for a block to display nodes by the same author for Drupal 6.

<?php
$nlimit = 15; //max nodes to show, edit this value
$node_type = 'story'; //type of node to display in this block

if (arg(0) == 'node' && is_numeric(arg(1))) {
    $output = null;
    $nid = arg(1);
    $node = node_load(array('nid' => $nid));
    $author = $node->uid;
    $result = db_query("SELECT n.created, n.title, n.nid, n.status, n.type
    FROM node n
    WHERE n.uid = $author AND n.status = 1 AND n.type = '$node_type' AND n.nid not in ('$node->nid')
    ORDER BY n.created DESC
    LIMIT $nlimit");
        $output = '<ul id="authored_nodes" class="menu">';
        while ($obj = db_fetch_object($result)) {
            $output .= '<li class="leaf">';
            $output .= l(($obj->title), "node/".$obj->nid);
            $output .= '</li>';
        }
        $output .= '</ul>';

    echo $output;
}
?>

Trouble is, it is for only one node type, and that, for me anyway, is not very useful. I don't want to have to have 4 or 5 different blocks for different node types. How would you modify the above code for multiple node types?

Comments

jastraat’s picture

This would be super easy to accomplish with views. You could just pass in the node author as the default argument and display the view on any pages where you wanted to see other nodes with the same author.

nevets’s picture

I would also recommend views but if you want to use the snippet but do not care about node type remove n.type = '$node_type' AND.

WorldFallz’s picture

Yep, i usually recommend views for this type of thing but if you really want to code it manually just change the following line:

$result = db_query("SELECT n.created, n.title, n.nid, n.status
  FROM node n
  WHERE n.uid = $author AND n.status = 1 AND n.nid not in ('$node->nid')
  ORDER BY n.created DESC
  LIMIT $nlimit");
jfmoore’s picture

Thanks, bur I'm not sure what you mean. If you replace this line:

WHERE n.uid = $author AND n.status = 1 AND n.type = '$node_type' AND n.nid not in ('$node->nid')

with this one:

WHERE n.uid = $author AND n.status = 1 AND n.nid not in ('$node->nid')

...it doesn't work, and if you replace these lines:

WHERE n.uid = $author AND n.status = 1 AND n.type = '$node_type' AND n.nid not in ('$node->nid')
    ORDER BY n.created DESC
    LIMIT $nlimit");

with these lines:

$result = db_query("SELECT n.created, n.title, n.nid, n.status
  FROM node n
  WHERE n.uid = $author AND n.status = 1 AND n.nid not in ('$node->nid')
  ORDER BY n.created DESC
  LIMIT $nlimit");

...it doesn't work, either. Can you please elaborate?

WorldFallz’s picture

Replace the original statement from the code you posted:

$result = db_query("SELECT n.created, n.title, n.nid, n.status, n.type
  FROM node n
  WHERE n.uid = $author AND n.status = 1 AND n.type = '$node_type' AND n.nid not in ('$node->nid')
  ORDER BY n.created DESC
  LIMIT $nlimit");

with the snippet I posted:

$result = db_query("SELECT n.created, n.title, n.nid, n.status
  FROM node n
  WHERE n.uid = $author AND n.status = 1 AND n.nid not in ('$node->nid')
  ORDER BY n.created DESC
  LIMIT $nlimit");
jfmoore’s picture

That works fine. Thanks. Turns out, though, that it's too broad in that it includes all node types. What if you wanted to specify just certain node types to be displayed and not others? How would you display, for example, story, page, and blog node types alone (and, of course, any other types one might want to later add?

WorldFallz’s picture

Then add some 'AND n.type =' pieces to the sql.

jfmoore’s picture

Like this?:

$result = db_query("SELECT n.created, n.title, n.nid, n.status
  FROM node n
  WHERE n.uid = $author AND n.status = 1 AND n.nid not in ('$node->nid') AND n.type = story AND n.type = page AND n.type = whatever
  ORDER BY n.created DESC
  LIMIT $nlimit");

Is this correct?