By jo1ene on
$terms = taxonomy_node_get_terms(arg(1)); //get terms from current node
foreach($terms as $term){
$tids[] = $term->tid;
}
$matches = db_result(db_query(db_rewrite_sql("SELECT nid FROM {node} INNER JOIN {Term_node} ON node.nid = term_node.nid WHERE node.type = 'foo' AND term_node.tid = $tids"))); //$tids???
I am finding the terms associated with the current node in the first statement - BTW in my case, there should always be 2 terms. I then take those terms, and want to find out how many nodes of type "foo" also match those (2) terms. I know this code is wrong (particularly the WHERE stuff) as I have never studied SQL in earnest. I just put a best effort forward for the sake of discussion.
Based on the number of matches eg. count($matches), I will do one of two things. That part I understand (more).
Anyone???
Comments
This should get you going
A couple of things since node and term_node have nid has a field you need to select with node.nid or term_node.nid, the example below aliases node and term_node and selects n.nid. Also to match to one of several values of tid you want 'tid IN (some set)' where (some set is a comma seperated list of values, we get the values by taking the array $tids and converting it to a comma seperated string with implode().
Wow!
This is what I was aiming for. I put this in a node with PHP filter to see what the output was - to see if it was finding the nids I wanted. It was indeed finding nodes of the right node type that matched the terms.
But it appears to be doing an OR match rather than an AND match. So I get a list of nids - some which match one term, some that match the other, and one that is listed twice because it matches both. What I would expect to come up is ONLY ONE nid - the one that is listed twice - because that is the only node (in this test case) that matches BOTH terms. So the query should be finding nodes that match both tids in the set.
I am going to try and figure this out more. Thanks for your input. I feel a lot closer to solving my issue.
Advanced Web Design
<?php$matches =
This gives me more hwat I'm looking for although I don't understand why. Accident? I'll keep testing.
Advanced Web Design
This works for me...
The real issue is that I don't care what the matches are, only that there is at least one node that matches both terms. By doing a regular SELECT and a DISTICT - and finding a difference in the counts, if any - I can see if there are any nids that come up twice.
In the end, I am trying to match ads using the advertisement module and views module. By using this in the arguments code for Term: Term ID, I can either pass tids with ',' or '+' to get AND or OR matching where appropriate.
This code is probably (definately) way clunky, but it makes the client happy for now. Any cleanup is appreciated.
Advanced Web Design
If you expect 2 and always just 2 terms this should work
If you expect 2 and always just 2 terms this should work
Is there a way to do this
Is there a way to do this using views and panels now?