I found this problem when using theme_table() to create a sortable table from a query that uses db_select's addExpression() method. It should output a 3 column table. The middle column's content should be the alias created by addExpression(). Instead, the content always ends up in the last column of the table, no matter what.
<!--break-->
I have tried this with any number of columns, and any alias added by addExpression() always ends up at the right-hand side of the table. I don't know if this is a problem with db_select or theme_table().
Details on my platform: PHP 5.2.14, PostgreSQL 8.3.11, Apache2 2.2.11
Sample code:
// $headers for theme_table and TableSort extender
$headers = array(
array( 'data' => 'Manufacturer', 'field' => 'company_name', 'sort' => 'asc' ), // FIRST column
array( 'data' => 'Item', 'field' => 'itemlink'), // SECOND column
array( 'data' => 'Abbr.', 'field' => 'abbreviation') // THIRD column
);
// Set up the query
$query = db_select('battery_model_list_view');
$query = $query->extend('TableSort');
$query = $query->orderByHeader($headers);
$query->addField('battery_model_list_view', 'company_name'); // FIRST db column
$query->addExpression("'<a href=\"/batt/details/' || batt_model_id ||'\">' || model || '</a>'", 'itemlink'); // SECOND db column
$query->addField('battery_model_list_view', 'abbreviation'); // THIRD db column
// Execute the query
$result = $query->execute();
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
// Create $table variables for theme_table()
$table = array(
'header' => $headers,
'rows' => $rows,
'caption' => null,
'attributes' => array(),
'colgroups' => array(),
'sticky' => null,
'empty' => null,
);
$out = "<p>The following is a list of items. Click the model to see details of that model</p>";
$out .= theme_table($table);
return $out;