moduledrupaldrupal-taxonomy

(drupal)a difficulty code to understand,get the same article's title under the same term


if ($node->taxonomy) {
 $query = 'SELECT DISTINCT(t.nid), n.nid, n.title FROM {node} n INNER JOIN {term_node}    t ON n.nid = t.nid WHERE n.nid != %d AND (';


 $args = array($node->nid);
   $tids = array();

  foreach ($node->taxonomy as $term) {
  $tids[] = 't.tid = %d';
  $args[] = $term->tid;
}

  $query .= implode(' OR ', $tids) .  ')';

 $result = db_query_range($query, $args, 0, 10);
while ($o = db_fetch_object($result)) {
echo l($o->title, 'node/' . $o->nid);
}

}

the code is from a drupal guru. . used to get the article's title under the same term in node.tpl.php, i have researched it two days, although know some part of it. the principle of the code i still don't know. expect someone can explain more details about it for me .many thanks.


Solution

  • Short version:

    It gets the array of tags of the node, retrieves the first 10 nodes that use at least one of these tags and outputs a link for each of these 10 results.


    Detailed version:

    First of all, the variable "$node" is an object that contains the data about a specific node (e.g. a Page or Story node). For example, "$node->title" would be the title of that node.


    "$node->taxonomy" tests is that node is tagged (because if it has no tags, it cannot retrieve the other nodes using the same tag(s). When there is one or several tags associated with that node/page/story, $node->taxonomy is an array .


    Now about the SQL query: "node" is the database table that stores the base fields (non-CCK) of every node. "term_node" is the database table that contains the combination of tag (which is called a "taxonomy term") and node.


    In both tables, "nid" is the "unique Node ID" (which is an internal autoincremented number). Because this column is in both tables, this is how the tables are joined together.

    In "term_node", "tid" is the "unique Term ID" (which is also an internal autoincremented number).


    The "node" table is aliased "n", therefore "n.nid" means "the Node ID stored in table node". The "term_node" table is aliased "t", therefore "t.tid" means "the Term ID stored in table term_node".


    The "foreach" loop goes thru the array of tags to extract the TermID of each tag used by the node in order to add it in the SQL query, and implode converts to a string.

    The loop stores a piece of SQL query for each tag in variable $tids and stores the actual value in variable $args because Drupal database calls are safer when the arguments are passed separately from the SQL query: "%d" means "integer number".


    "db_query_range" is a function that selects multiple rows in the database: here, "0 10" means "retrieve the first 10 results".


    "db_fetch_object" in the "while" loop retrieves each result and stores it in the variable "$o", which is an object.

    Therefore "$o->title" contains the value of the column "title" retrieved by the SQL query.


    The function "l" is the drupal functin that creates the code for an HTML link: the first argument is the name of the link, the second argument is the drupal path: in Drupal, any node can be accessed by default using "www.yoursite.com/node/NodeID", which is why it gives the path "node/123" (where 123 is the "Node ID").

    This function is useful because it transparently handles custom paths, so if your node has a custom path to access it using "www.yoursite.com/my-great-page" instead, it will create a link to that page instead of "www.yoursite.com/node/123" automatically.