开发者

Drupal & PHP/MySQL : How to compare fields with current user's field?

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

Let's say that userA has created a co开发者_运维技巧ntent 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 :)


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.


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..
}

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜