0
$attr = explode(",", $node->field_xlnum[0]['value']);

$node->field_xlnum[0]['value'] contains 20,4,56,987,68,96, where each value is a node ID. I want to get all the titles, and output them in an HTML list. How do I do it?

I tried the following code, but it doesn't work.

$attr = explode(",", $node->field_xlnum[0]['value']);
$result = db_query("SELECT nid, title FROM {node} WHERE nid IN ($attr) AND type = %s order by created DESC", test);
while(($items = db_fetch_object($result)) { 
  $output = theme_item_list($items, array('class'=>'tabs primary'));
}

echo $output;
0

Following up on what tostinni posted, this is what worked for me. I also added the $title parameter. It's supposed to be the second parameter, and if you leave it out, the array('class'=>'tabs primary') would be used as the second parameter instead, which wouldn't work quite right. I simplified the query a little since I don't have a content type called 'test,' but you can put that in easily:

// the $attr should be a string
$attr = $node->field_xlnum[0]['value'];
// $attr put into a placeholder
$result = db_query("SELECT title FROM {node} WHERE nid in (%s) order by created DESC", $attr);

/*** 
  * if you put theme_item_list into the while loop, 
  *  it will create separate lists for each item,
  * instead of adding 'li's to one ul, as I'm assuming you want?
  */
while($items=db_fetch_object($result)){
// put the title strings into an array
    $list[] = $items->title;
}
// including the $title parameter - if not needed, you can change to $title = NULL
$output = theme_item_list($list, $title = 'A list', 'ul class="tabs primary"');
echo $output;
  • while($items=db_fetch_object($result)){ // put the title strings into an array $list[] = $items->title; } should be enough. No need for $i. – nonsenz Jun 13 '11 at 5:35
  • For sure - edited to remove $i incrementer. – g_thom Jun 13 '11 at 15:01
3

A few remarks:

  1. You said that $node->field_xlnum[0]['value'] contains a list of nid separated by comas so you don't need to explode it in order to use them in a IN clause. If you do so, $attr will be an Array().
  2. You put test as an argument in your query, isn't this a variable $test or a string 'test'
  3. You should better use db_placeholders for putting multiples values inside a IN.

    $attr = explode(",", $node->field_xlnum[0]['value']);
    db_query("SELECT n.nid, n.title
      FROM {node} n
      WHERE n.nid IN (" . db_placeholders($attr, 'int') . ")
      AND type = %s 
      ORDER BY n.created DESC", $data, 'test'
    );
    

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.