Table view with rows/columns switched
Last modified: November 6, 2009 - 14:53
This views snippet allows you to render a tableview with columns and rows switched. Suppose you have the following output:
Title | Price 1 | Price 2
----------------------------
Node #1 | $10 | $20
Node #2 | $15 | $25but what you really want is the node titles displayed in the header, and the prices as the rows:
| Node #1 | Node #2
----------------------------
Price 1 | $10 | $15
Price 2 | $20 | $25Obviously you cannot create a view using nodes as the header, because they are dynamic data.
Drupal 5
Instead, use the following code in your theme's template.php file to render the table with rows and columns switched:
<?php
function YOURTHEME_views_view_table_VIEWNAME($view, $nodes, $type) {
$fields = _views_get_fields();
// Table header
// Reserve first column for labels
$header = array('');
// Render first field of all nodes
foreach ($nodes as $node) {
$cell['data'] = views_theme_field('views_handle_field', $view->field[0]['queryname'], $fields, $view->field[0], $node, $view);
$cell['class'] = 'view-cell-header '. views_css_safe('view-field-'. $view->field[0]['queryname']);
$header[] = $cell;
}
// Nuke first field, so it won't get displayed in the rows anymore
unset($view->field[0]);
// Table rows
$rows = array();
foreach ($view->field as $field) {
$row = array();
// First column is just the label
$cell['data'] = $field['label'];
$cell['class'] = views_css_safe('view-field-'. $field['queryname']);
$row[] = $cell;
// Add corresponding column for all nodes
foreach ($nodes as $node) {
$cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
$cell['class'] = views_css_safe('view-field-'. $field['queryname']);
$row[] = $cell;
}
$rows[] = $row;
}
return theme('table', $header, $rows, array('class' => 'show-table'));
}
?>Drupal 6
In drupal 6 when creating your view select 'table' as display style.
Create the following views template if you wish to switch the column and headers:
<?php
$row = array();
foreach ($rows as $col){
foreach ($col as $ltr => $value){
$row[$ltr][] = $value;
}
}
$first = TRUE;
$element = 'odd';
?>
<table class="<?php print $class; ?>">
<?php if (!empty($title)) : ?>
<caption><?php print $title; ?></caption>
<?php endif; ?>
<?php if ($first):?>
<thead>
<tr class="<? echo $element; ?>">
<th>
</th>
<?php foreach($row['title'] as $title): ?>
<th>
<?php echo $title; ?>
</th>
<?php endforeach; ?>
</tr>
</thead>
<?php $first = FALSE;
endif; //$first
$element = 'even';
if (!$first): ?>
<tbody>
<?php foreach ($row as $cck_field => $rowname):?>
<?php if ($cck_field != title): ?>
<tr class="<? echo $element; ?>">
<th>
<?php echo $header[$cck_field]; ?>
</th>
<?php foreach($rowname as $count => $item): ?>
<td>
<?php echo $item; ?>
</td>
<?php endforeach; ?>
</tr>
<?php
if ($element == 'odd'){
$element = 'even';
} else {
$element = 'odd';
}
endif; //cck_field != title
endforeach; ?>
</tbody>
<?php endif; //!first ?>
</table>Save it as views-view-table.tpl.php in the theme's directory.
This only works if you wish to have the node titles as the table headers.

do you have to define your
do you have to define your custom View's name in the file name somehow? How?