Hello,

I need to run a custom sql query and display it in a table what is the best way to do this?

Comments

JohnnyMoney’s picture

Ok, haven't done this myself yet but maybe the VIEWS module allows this. Have a look at the module just in case.

drupdk’s picture

There is a lady called Nancy ( She has made the drupal cookbook I think) she helped me like this.
Create a page and put this into it.


$sql = 'SELECT sted, selskab, vej, username FROM {test}';
$header = array(
array('data' => 'sted', 'field' => 'sted'),
array('data' => 'selskab', 'field' => 'selskab'),
array('data' => 'vej', 'field' => 'vej'),
array('data' => 'username', 'field' => 'username'),
);

$sql .= tablesort_sql($header);

$result = pager_query($sql, 50);

while ($data = db_fetch_object($result)) {
$rows[] = array($data->sted, $data->selskab, $data->vej, $data->username, str_repeat('--',$data->d).$data->title);
}

if (!$rows) {
$rows[] = array(array('data' => t('Empty at the moment..'), 'colspan' => '4'));
}

echo theme('table', $header, $rows);
echo theme('pager', NULL, 50, 0);

Is it something like this ?

colin_e’s picture

Thanks stbdk, that was incredibly helpful. Finally helped me crack a problem I had been grappling with for days (how to build a View, or a View-like table, based on a custom query).

stif’s picture

This helped me a lot as well, thanks for sharing your idea.

caver456’s picture

Hi - I think I figured out at least part of what you're looking for, in the context of the views module in drupal6.10

http://drupal.org/node/409808