Function to get a list of node object
ananto - April 22, 2008 - 04:07
I know there is a drupal function node_load to get a node object from a given nid or param. I can use node_load to retrieve a single node object.
But I want to get an array of node object from a given param. Example : I want an array of node object that had a node type "story", the param would be array ('type'=>'story') . Is there any drupal function to do this ? Is there any other way instead of querying directly to the table ?
Thx before.

You could use the views
You could use the views module.
In this case I don' want to
In this case I don't want to use views module, because I will use this to build a custom module.
Then...
Then the short answer to your question is 'no.' nevets gave you the right answer, insofar as what you want is the result of an SQL query. The node_load function loads/returns a single node object, whereas an SQL query will return an array of pointers to such (stored) objects. Using the Drupal database layer is the most efficient way to do what you want.
Cheers,
efolia
OK, thanks for helping me
OK, thanks for helping me guys.
PHP treats objects and
PHP treats objects and arrays similarly. If you use node_load then you a can type set it to an array just by setting it so.
<?php$myobject = node_load(array('nid'=> 1));
print_r($myobject);
$myarray = (array)$myobject;
print_r($myarray);
?>
Also the node type is in the object so if you want you can get the type by looping through the object using foreach().
Hiveminds Magazine | FireOrb | Drupal Street | Drupal offline manual
I guess node_load only
I guess
node_loadonly produce a single node object, not an array of node object.I want to get list of node object not only a single node object.
Thx.
Ahh, I understand. Nope
Ahh, I understand. Nope there is no finished node_load for this which is one of weaknesses in using the default Drupal function. What happens if you call node_load too many times in a loop is not very pretty and has been known to kill a server. It is one of the main reasons that Drupal.org needs such heavy server infrastructure.
Try using the node_queue module. It has several functions that group, randomize and sort nodes. That or write your own custom queries.
Hiveminds Magazine | FireOrb | Drupal Street | Drupal offline manual
Kill a server ? Wow...
Kill a server ? Wow... Thanks for telling me.