1

I have created a content Type Student, I have imported the list of student with node import. Now I want to get the name, location and other values of the content type Student from my database. How can I do that with PHP? I have already created a simple module with a menu and block.

4

Why don't use Views module. This is exactly the purpose of this module. And if you are not so aware of all drupal's API, it will really save you a lot of time, and security in your website.

This tool is essentially a smart query builder that, given enough information, can build the proper query, execute it, and display the results. It has four modes, plus a special mode, and provides an impressive amount of functionality from these modes.

Among other things, Views can be used to generate reports, create summaries, and display collections of images and other content.

  • OK, but how can I retrieve the view. Because I need the values of the student. I'm creating a genetic algorithm that assigns students to a internship. (automatic placement planning tool) Thanks for your friendly reply! – user001 Mar 29 '11 at 14:45
  • What is your question there ? Is it about "how do a display my view" ? – Haza Mar 29 '11 at 14:52
  • Yes, how can I get my view in my drupal module in PHP. – user001 Mar 29 '11 at 14:59
  • The purpose of a views is just to display contents. Views can generate page or blocks by default. You need to add a new "display" in views UI to be able to see that. For a "page display", you need to specify a page URL, for a block display, it generate a block that is available in the blocks section, in the admin. If you really want to "call" the views in a module, you should check the views_embed_view() function. – Haza Mar 29 '11 at 15:03
  • The question is not about displaying the students, but to retrieve their information. Presumably for some process (internship assignment). – Pierre Buyle Mar 30 '11 at 11:02
5

To retrieve a Student node (in OO-lingo that would a Student instance), use node_load(). If you want to retrieve all Student nodes, you have to loop over the result of a database query like this

$results = db_query(db_rewrite_sql("SELECT nid FROM {node} WHERE type = 'student'"));
while($nid = db_result($results)) {
  $node = node_load($nid);
  // Do something with $node
}

If you have a lot of Student nodes, it's probably not a good idea to process all of them in a single run. If you really need to process all of them, you should use the Batch API.

  • Finaly the solution! Thanks! But, for the last time - how can I access these values from my student? Many thanks! – user001 Mar 30 '11 at 11:44
  • And how can I copy all the values in $node into an array of strings? – user001 Mar 30 '11 at 12:04
  • $node will be an object with the student data as properties, there is no need to copy them to a string or an array to use them. Install the Devel module and use dpm() to output $node and inspect its structure. – Pierre Buyle Mar 30 '11 at 13:07

Your Answer

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