Jump to content

Drupal Modules


Craig Hopson

Recommended Posts

hi guys i'm stuck i'm trying to query in drupal, here is what works

function browse_output() {   $query = db_select('users', 'd')->extend('PagerDefault');  $query->fields('d', array('uid', 'name', 'picture'));  $result = $query    ->limit(10)     ->orderBy('d.uid')    ->execute();  $rows = array();  foreach ($result as $row) {    $rows[] = array('data' => (array) $row);  }  $build['pager_table'] = array(    '#theme' => 'table',    '#rows' => $rows,    '#empty' => t('There are no date formats found in the db'),  );  $build['pager_pager'] = array('#theme' => 'pager');  return $build;}

and this displays a table with user id, name, and picture(number)ok i know it works but i dont understand what is going on in the code and why cant i do it like this

$result = mysql_query("SELECT * FROM users");while($row = mysql_fetch_array($result))  {  echo $row['picture']." ".$row['id']." ".$row['name'];  echo "<br />";   }

ANY advice on this would help and yes i've read the documentation but cant make much sence from it i got the function browse_output from the examples on the Drupal site Thanks

Link to comment
Share on other sites

The two pieces of code aren't the same, the one above does (or at least supports) a lot more than select all columns in all rows and printing things out. The database class they're using looks like it supports paging and limiting, ordering, and presumably parameterized queries. Not needing to use a specific extension like mysql or mysqli in your code also means that you can change the database implementation in the backend without rewriting your code. Here is the documentation for it: http://api.drupal.or...roup/database/7

Link to comment
Share on other sites

Thanks for the quick responce i have read that but really dont understand. The top script does support paging and limiting and puts results in a table i dont want all that just yet i want to learn...do you know/Can you convert top script into its most basic form ie the bottom script

Link to comment
Share on other sites

That function is used in conjunction with other code. It doesn't display anything, it returns an array data structure that looks like it contains some meta-data in addition to the results from the database. It should define $build as an array first before adding elements to it, but the result of that function is to return a data structure that contains the database records in addition to some other data. I assume that whatever code calls that function counts on the rest of the data being there also.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...