(drupal)一个易于理解的难度代码,在同一术语下获得同一篇文章的标题

发布于 2024-10-03 13:43:23 字数 631 浏览 4 评论 0原文

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);
}

代码来自 Drupal 大师。 。用于在node.tpl.php中的同一术语下获取文章的标题,我研究了两天,尽管知道其中的一部分。代码的原理我还是不知道。期望有人能为我解释更多细节。非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀里藏娇 2024-10-10 13:43:23

简短版本:

获取节点的标签数组检索至少使用这些标签之一的前 10 个节点,并且 >为这 10 个结果中的每一个输出一个链接


详细版本

首先,变量“$node”是一个对象,包含有关特定节点(例如Page或Story节点)的数据。
例如,“$node->title”将是该节点的标题。


$node->taxonomy”测试是节点被标记(因为如果它没有标签,则无法检索使用相同标签的其他节点。
当有一个或多个标签与该节点/页面/故事关联时, $node->taxonomy 是一个 array


现在关于 SQL 查询:
node”是存储每个节点的基本字段(非CCK)的数据库表。
term_node”是包含标签(称为“分类术语”)和节点组合的数据库表。


在这两个表中,“nid”是“唯一节点 ID”(这是一个内部自动递增数字)。因为该列位于两个表中,所以这就是将表连接在一起的方式。

在“term_node”中,“tid”是“唯一的Term ID”(也是一个内部自动递增的数字)。


node”表的别名为“n”,因此“n.nid”表示“存储在表节点中的节点 ID”。
term_node”表的别名为“t”,因此“t.tid”表示“存储在表 term_node 中的术语 ID”。


foreach”循环遍历标签数组,提取节点使用的每个标签的TermID,以便将其添加到 SQL 查询中,并且 implode 转换为一个字符串。

该循环将每个标签的一段 SQL 查询存储在变量 $tids 中,并将实际值存储在变量 $args 中,因为单独传递参数时,Drupal 数据库调用会更安全从 SQL 查询中:“%d”表示“整数”。


db_query_range”是一个在数据库中选择多行的函数:这​​里,“0 10”表示“检索前 10 个结果”。


while”循环中的“db_fetch_object”检索每个结果并将其存储在变量“$o”中,该变量是一个对象。

因此,“$o->title”包含 SQL 查询检索到的“title”列的值。


函数“l”是创建 HTML 链接代码的 drupal 函数:第一个参数是链接的名称,第二个参数是 drupal 路径:在 Drupal 中,任何节点都可以是默认情况下使用“www.yoursite.com/node/NodeID”访问,
这就是为什么它给出路径“node/123”(其中123是“Node ID”)。

此函数非常有用,因为它透明地处理自定义路径,因此如果您的节点有自定义路径来使用“www.yoursite.com/my-great-page”访问它,它将创建一个链接自动转到该页面,而不是“www.yoursite.com/node/123”。

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.

假面具 2024-10-10 13:43:23

我不会称写这篇文章的人为大师,你可以做得更漂亮。无论如何,他所做的就是创建一个如下所示的查询:

 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 (t.tid = %d OR t.tid = %d OR ... t.tid = %d);

最终结果是他选择与所选节点至少共享一个术语但不是节点本身的所有节点 ID 和标题(仅一次)。

I wouldn't exactly call the guy who wrote this a guru, you could do this a lot prettier. Anyways what he does it create a query that looks like this:

 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 (t.tid = %d OR t.tid = %d OR ... t.tid = %d);

The end result is that he selects all the node ids and titles (only once) that share at least one term with the selected node, but isn't the node itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文