Note: This functionality is the Views 3/Drupal 7 equivalent of this older snippet (with some extra features added).
This functionality has also been turned into a module called Views Flipped Table for Drupal 6 and 7.
This Views snippet allows you to render a table format with the columns and rows switched.
Suppose you have the following output (a table with each row being a node):
Title | Price 1 | Price 2
----------------------------
Node #1 | $10 | $20
Node #2 | $15 | $25
but what you really want are the nodes displayed as columns, and the different prices as the rows:
| Node #1 | Node #2
----------------------------
Price 1 | $10 | $15
Price 2 | $20 | $25
Save this code as a Views template file, named 'views-view-table.tpl.php' (or a variation thereof), in your theme directory.
You can define any field in the view to be displayed as the column headings by simply giving them a label of 'Column Heading' (i.e. the column headings aren't restricted to the node titles).
You can also define a field in the view whose value will be used as the class name for each cell in its column. This would be useful if you wanted to highlight the 'Node #2' column in the example above using CSS. Simply add a label of 'Column ID' to the field.
Flipped table with title as table caption
<?php
/**
* @file views-flipped-table.tpl.php
* Template to display a view as a table with rows and columns flipped.
*
* - $title : The title of this group of rows. May be empty.
* - $header: An array of header labels keyed by field id.
* - $fields: An array of CSS IDs to use for each field id.
* - $class: A class or classes to apply to the table, based on settings.
* - $row_classes: An array of classes to apply to each row, indexed by row
* number. This matches the index in $rows.
* - $rows: An array of row items. Each row is an array of content.
* $rows are keyed by row number, fields within rows are keyed by field ID.
*
* @ingroup views_templates
*/
?>
<?php
// Flip the table.
$row = array();
foreach ($rows as $col){
foreach ($col as $ltr => $value){
$row[$ltr][] = $value;
}
}
$first = isset($row['title']);
$element = 'odd';
?>
<table class="<?php print $class; ?>">
<?php if (!empty($title)) : ?>
<caption><?php print $title; ?></caption>
<?php endif; ?>
<?php if ($first) : ?>
<thead>
<tr class="<?php 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';
?>
<tbody>
<?php foreach ($row as $field => $rowname) : ?>
<?php if ($field != 'title') : ?>
<tr class="<? echo $element; ?>">
<th>
<?php echo $header[$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';
}
?>
<?php endif; // field != title ?>
<?php endforeach; ?>
</tbody>
</table>
Flipped table without caption thead
<?php
/**
* @file views-flipped-table.tpl.php
* Template to display a view as a table with rows and columns flipped.
*
* - $title : The title of this group of rows. May be empty.
* - $header: An array of header labels keyed by field id.
* - $fields: An array of CSS IDs to use for each field id.
* - $class: A class or classes to apply to the table, based on settings.
* - $row_classes: An array of classes to apply to each row, indexed by row
* number. This matches the index in $rows.
* - $rows: An array of row items. Each row is an array of content.
* $rows are keyed by row number, fields within rows are keyed by field ID.
*
* @ingroup views_templates
*/
?>
<?php
// Flip the table.
$row = array();
foreach ($rows as $col){
foreach ($col as $ltr => $value){
$row[$ltr][] = $value;
}
}
$first = isset($row['title']);
$element = 'odd';
?>
<table class="<?php print $class; ?>">
<?php if (!empty($title)) : ?>
<caption><?php print $title; ?></caption>
<?php endif; ?>
<?php if ($first) : ?>
<!-- <thead>-->
<!-- <tr class="--><?php //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';
?>
<tbody>
<?php foreach ($row as $field => $rowname) : ?>
<!-- --><?php /*if ($field != 'title') : */?>
<tr class="<? echo $element; ?>">
<th>
<?php echo $header[$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';
}
?>
<!-- --><?php /*endif; // field != title */?>
<?php endforeach; ?>
</tbody>
</table>
Comments
throws an 'undefined index @ line 36' error
thanks for this -- it displays fine, except that it displays an 'undefined index @ line 36' error.
fix for undefined index error
if anyone else experiences this error, tweaking line 36 worked for me:
from this
$row[$field][$node[$col_id]] = $value;
to
$row[$field][$col_id] = $value;
Only one node showing
in combination with a flag=true filter, when using above template, only one node shows up. any idea whether its a flag or a views issue?
having that too. used this
having that too. used this inseatd
A bit editing from me
The code only displays one item, plus error on line 36.
I modified it a bit like this:
keep both table designs
is it possible to add this table view with rows/columns switched without overwriting the old design and keep both?
Having some trouble...
Would love for this to work however I get the following errors when viewing the view
Any ideas?
Cheers
On the internet, nobody knows you're a dog
Similar Problem
I have similar problem.
any ideas
I also had this problem, but
I also had this problem, but I managed to find a solution. Here's my version of the file.
Thx very much blade_A-web -
Thx very much blade_A-web - this works well for me, but the labels on the y-axis (product names) have disappeared.
Thx very much,
grateful
CSS fix
Great post - really helped me on a project that is heavily using views. The only issue I had was not being able to use css to style the td classes because they were numbers. Changed line 68 from this:
to this:
and then was able to style with css using td.c1, td.c2. etc.
works fine till except for empty column.
This works fine for me however, I have columns that I would like to hide when they are empty. When I make the settings change in views>>myview>>format>>table>>settings it saves fine.
However, when I look at the views that should shows multiple nodes, its show the same node repeated. (So if there were meant to be two different nodes it shows the same node twice)
Have done a bit more digging and it maybe related to this.
http://drupal.org/node/1371252
Found the solution simply
Found the solution simply needed to upgrade views from 7.x-3.3 to 7.x-3.5
D8 Version
I'm going to be working on a D8 version of this. If anyone has tried this already, I'd love to see how you did it. It seems as though the Flipped Views module has no current path for a D8 version.
When I get it done, I'll post the code here.
Flip table in Drupal 8
Here is the code to flip table in Drupal 8, there is no need to change twig template, just add a theme preprocess function.