Drupal 和PHP/MySQL:如何将字段与当前用户的字段进行比较?

发布于 2024-11-25 22:55:13 字数 745 浏览 0 评论 0原文

我有一个问题,与 drupal 中的 php 语法/mysql 有关:

假设 userA 创建了一个名为“test”的内容类型,他在字段 field_example 中填充了值“xxx”。随后,另一个用户 userB 创建了另一个内容,并使用相同的值“xxx”填充相同的字段 field_example

我想知道如何仅使用创建的节点(其中字段 field_example 与当前用户相同)显示视图?我正在使用的内容类型“测试”中没有(并且我不想)用户引用。

我已经查看了 View PHP Filter,但我想知道如何比较字段的值?这是我的尝试 [我不是 PHP 专家,您可能会注意到:)]:

<?php

$a="SELECT uid FROM users WHERE uid=%d";

/* ??? How to create $b which can get value of field_example from content_type_test from current user which is logged in ? */

$b="";

$c="SELECT field_example FROM content_type_test";

if ($b==$c){
echo "Ok, I've what I want :) ";
}

?>

任何帮助将不胜感激,因为我已经有一段时间在寻找有关此查询的信息了...

谢谢大家:)

I've an issue, which is about php syntax/mysql in drupal:

Let's say that userA has created a content type called "test" where he filled the field field_example with value "xxx". Afterwards, another user, userB has created another content and filled the same field field_example with the same value "xxx".

I'd like to know how is it possible to display a view only with the node created where the field field_example is the same for the current user ? I don't have (and i don't want) a user reference in the content type "test" i'm using.

I've looked through View PHP Filter, but i'm wondering how to compare values of field ? Here's my attempt [i'm not an expert in PHP as you'll might notice :) ] :

<?php

$a="SELECT uid FROM users WHERE uid=%d";

/* ??? How to create $b which can get value of field_example from content_type_test from current user which is logged in ? */

$b="";

$c="SELECT field_example FROM content_type_test";

if ($b==$c){
echo "Ok, I've what I want :) ";
}

?>

Any help would be greatly appreciated since it's been a while i'm looking for information about this query...

Thanks all :)

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

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

发布评论

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

评论(2

阳光下的泡沫是彩色的 2024-12-02 22:55:13

我没有视图模块解决方案。

我想到的一个想法是,如果用户 A 提交多个节点怎么办,那么您会使用哪个示例值?另外,您使用的 Drupal 版本是什么?

假设用户只会提交这种类型的一种内容,并且您正在运行 Drupal 6(我从代码示例中猜测),那么它可能看起来像这样:

<?php
// current user
global $user;
// select this user's node
$nid = db_result(db_query("SELECT nid FROM {node} WHERE type = 'my_content_type' AND status = 1 AND uid = %d", $user->uid));
// if this node loads fine, then proceed with the rest
if ($node = node_load($nid)) {
  // find nodes with the same example value, which do not belong to the current user
  $result = db_query("SELECT nid FROM {node}
                        INNER JOIN {content_type_test} ctt ON n.vid = ctt.vid
                        WHERE n.status = 1 AND ctt.field_example_value = '%s' AND uid <> %d
                        ORDER BY n.created DESC", $node->field_example[0]['value'], $user->uid);
  // loop through results
  while ($row = db_fetch_object($result)) {
    $node = node_load($node->nid);
    // append the teaser output (if this is what you want to do)
    $output .= node_view($node, TRUE);
  }
  // print the output
  print $output;
}
else {
  print t('No other content found.');
}
?>

如果用户提交多个这些内容类型,那么就会有这是避免在这里写小说的另一个答案。有几种方法可以解决这个问题。

另外,如果这是 Drupal 7,我也会使用不同的函数。

I don't have a Views module solution.

One thought that comes to mind is what if User A submits multiple nodes, which example value would you use then? Also, what version of Drupal are you working with?

Assuming that a user will only ever submit one content of this type and you are running Drupal 6 (my guess from code examples), then it might look something like this:

<?php
// current user
global $user;
// select this user's node
$nid = db_result(db_query("SELECT nid FROM {node} WHERE type = 'my_content_type' AND status = 1 AND uid = %d", $user->uid));
// if this node loads fine, then proceed with the rest
if ($node = node_load($nid)) {
  // find nodes with the same example value, which do not belong to the current user
  $result = db_query("SELECT nid FROM {node}
                        INNER JOIN {content_type_test} ctt ON n.vid = ctt.vid
                        WHERE n.status = 1 AND ctt.field_example_value = '%s' AND uid <> %d
                        ORDER BY n.created DESC", $node->field_example[0]['value'], $user->uid);
  // loop through results
  while ($row = db_fetch_object($result)) {
    $node = node_load($node->nid);
    // append the teaser output (if this is what you want to do)
    $output .= node_view($node, TRUE);
  }
  // print the output
  print $output;
}
else {
  print t('No other content found.');
}
?>

If users submit more than one of those content types, then that would have to be another answer to avoid from writing a novel here. There's a couple ways to approach that.

Also if this was Drupal 7, I'd be using different functions as well.

森林很绿却致人迷途 2024-12-02 22:55:13

我假设你想要这样的东西:

function _get_list() {
global $user; // This is global variable, contains information for current logged in user.

$results = db_query("SELECT ctt.field_example FROM {node} n JOIN {content_type_test} ctt ON n.nid = ctt.nid AND n.vid = ctt.vid WHERE n.uid = %d", $user->uid);

while($row = db_fetch_object($results)){
   //$row->nid.. etc..
}

}

I assume you want something like this:

function _get_list() {
global $user; // This is global variable, contains information for current logged in user.

$results = db_query("SELECT ctt.field_example FROM {node} n JOIN {content_type_test} ctt ON n.nid = ctt.nid AND n.vid = ctt.vid WHERE n.uid = %d", $user->uid);

while($row = db_fetch_object($results)){
   //$row->nid.. etc..
}

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