Creating a list of similar entries/contents in Drupal.

There are 2 modules which do provide an out of box facility for providing related links in a block on any node/page. But neither of them solved my purpose –
Requirement: To create a list of nodes comprising of all the node titles of taxonomy term(s) of one category. (of course the node has more than one category)

 

Example:
A content type mobile has two categories
1. Price: Low, High, Medium
2. Brand: Nokia, Motorola, etc.

Requirement is to show a block only with the brand terms.
In Nokia N95 page all the other nokia models will be shown in a block.

After going through all I found one snippet on drupal.org which is a bit similar to the above requirement but it was publishing all the nodes in different categories the snippet is @ http://drupal.org/node/76923.

 

Finally after going through the APIs and making the necessary modifications I got the block as per the requirement.
The snippet is given below.
Change the parameters as per your requirement.
($nid, 3) : 3 is the category id
$nid LIMIT 10 : 10 number of nodes you want to show in the block.

<code>
<?php
if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
  $nid = (int)arg(1);
  $terms = taxonomy_node_get_terms_by_vocabulary($nid, 3);
 
 $output = "<ul>";
  foreach($terms as $term){
    $sql = "SELECT n.title, n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid = $term->tid AND n.nid != $nid LIMIT 10";  
    $result = db_query(db_rewrite_sql($sql));
    if (db_num_rows($result)) {
      $output .="<li>$term->name</li><ul>";
      while ($anode = db_fetch_object($result)) {
        $output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
      }  
      $output.="</ul>";
    }
  }
  $output .= "</ul>";
  return $output;
}
?>

</code>
 

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Based on: ManuScript | Optimized for Drupal :www.SablonTurk.com