Table view with rows/columns switched
Last modified: June 19, 2009 - 09:49
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--YOURVIEWNAME.tpl.php inside your theme/YOURTHEME folder.
This only works if you wish to have the node titles as the table headers.

i might be wrong, but i doubt
i might be wrong, but it seems that there is no $rows array. it seems to be the rendered view. at least in my D6 Views 2.x situation. i get errors that this code can not find a rows array:
warning: Invalid argument supplied for foreach() in /sites/all/themes/nitobe/views-view--compare.tpl.php on line 3.And line 3 sais:
foreach ($rows as $col)template
Please note that you should use views-view-table.tpl.php as the base template (when trying this in Drupal 6). Otherwise the variable
$rowsdidn't contain the results but the entire view (as bara.munchies said).Table view with rows/columns switched
hi friend
i have lot of problem for switching rows/columns in view
i have paste the code into views-view-table.tpl.php as you said but the problem still here
could you help me resolving this problem
ou know you need to copy the
ou know you need to copy the template into your directory and rename it according to your theme name?
Invalid argument supplied for foreach()
Invalid argument supplied for foreach()
i'm trying on it since yesterday and i can't figure the source of the problem
anyone could help me
Absolutely genious! This
Absolutely genious! This saved me a hell of a lot of time.