0
$result = db_query("SELECT {node}.title, {content_type_answer}.field_question_nid, {content_type_answer}.nid FROM {node}, {content_type_answer} ORDER by nid ASC LIMIT %d, %d", 0, 3);
while($ref = db_fetch_object($otvet)) {
    $otvets[] = array($ref->field_question_nid, $ref->nid, $ref->title);
}

I have 2 content types: 1. question, 2. answer. In {content_type_answer} i have a nodereference field - field_question_nid that gives me the node id of the referenced question and {content_type_answer}.nid - gives me the answer node id.

But the problem is in title, {content_type_answer} table doesn't has a title row. In a code above i've succesfully selected title row from {node} table :D But how to make condition - i need only {node}.title from nodes which have {content_type_answer}.nid?

  • you are describing a WHERE or a JOIN. I'd let Views do it for you as you have a couple other issues in your code that suggest you aren't entirely comfortable with writing db access code. – Jimajamma Dec 21 '11 at 17:35
  • Could you help with a piece of code? – Alexander Kim Dec 21 '11 at 18:13
  • 1
    Why are you coding it directly instead of using the Views Module? – Jimajamma Dec 21 '11 at 18:21
  • i'm doing that in page.tpl.php. Because with views i cant display question and below answer on that question (node reference to question) – Alexander Kim Dec 21 '11 at 18:35
  • 1
    OK, one way to do this is to create a view with a block display. Have this view return answers based upon a nid by using the nid as an argument. Since you can't pass an argument to a block, tell it to grab the default argument from the URL. Figure out what fields you want to display, and then set up via the Blocks Admin page to display this block on a page that displays a question. This will provide Views with the nid you need to display the answer to the question on the same page as the question. If you want to limit it to four answers, you can do that with views and its paging options. – Jimajamma Dec 21 '11 at 20:02
1

This is a job for Views. To expound upon the comments, this is a screen shot of a quick view that will do this:

enter image description here

I created two quick content types, answer and question. I used the body field just for expediency in both, and the only other field I added was a node reference called question in the answer type.

In the View, I filted on published nodes of type answer, the argument is that node reference field above, with the default coming from the URL. I output the Title and Body field. I set the paging options to 4 so that it would output a maximum of 4 answers to match the LIMIT in your query. I didn't sort the output but you can do that if you want in Sort Criteria.

I created only one quick question and answer. On my system, the question is nid 379, so:

enter image description here

putting that in as the argument, the view will output any answers (well, up to 4) with that nid as the node reference in question. Create a block display for this, and you can have this displayed on your node page with the question.

Now, as you mention in the comments, if you are sure you want to do all of this yourself in your page.tpl.php file with db_query()s, feel free to scrape out the query Views has built for you from the preview. In this case, that would be:

SELECT node.title AS node_title, node.nid AS nid, node_revisions.body AS node_revisions_body, node_revisions.format AS node_revisions_format FROM node node  LEFT JOIN content_type_answer node_data_field_question ON node.vid = node_data_field_question.vid LEFT JOIN node_revisions node_revisions ON node.vid = node_revisions.vid WHERE (node.status = 1) AND (node.type in ('answer')) AND (node_data_field_question.field_question_nid = 379 )

and modify it to your specific needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.