Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active August 17, 2022 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnau/68f5169661058beaa4f19ecbbfec3c05 to your computer and use it in GitHub Desktop.
Save xnau/68f5169661058beaa4f19ecbbfec3c05 to your computer and use it in GitHub Desktop.
Demonstrates a custom template for showing a Participant Database list in "reverse" form: rows as fields and columns as records.
<?php
/**
* @version 1.1
*
* PDbList template demonstrating how to show a "reverse" table with records as
* columns and fields as rows
*
*/
/** @var PDb_List $this */
// sets up our variables
$header_column = 'full_name'; // name of the field to use as the header title for each record
$table_rows = array(); // holds the row data
$header_row = array(''); // holds the header row as $title
/* this section fills the variables with the values from the list object
* in such a way as to be displayable in "reverse" format
*/
foreach ( $this->records as $record_id => $record_fields )
{
foreach ( $record_fields as $field )
{
/** @var PDb_Field_Item $field */
// if the current field is the header column field, put its value in the header row
if ( $field->name() === $header_column )
{
$header_row[] = PDb_FormElement::get_field_value_display( $field );
continue; // move on to the lext field
}
// if we're starting a new field row, store the field's title as the row header
if ( !isset( $table_rows[$field->name()] ) )
{
$table_rows[$field->name()] = array($field->title());
}
$table_rows[$field->name()][] = PDb_FormElement::get_field_value_display( $field );
}
}
?>
<div class="wrap <?php echo $this->wrap_class ?>" id="<?php echo $this->list_anchor ?>">
<?php
/** @see PDb_List::show_search_sort_form */
$this->show_search_sort_form();
?>
<table class="wp-list-table widefat fixed pages list-container" >
<?php if ( has_action( 'pdb-prepend_to_list_container_content' ) ) : ?>
<caption>
<?php do_action( 'pdb-prepend_to_list_container_content' ) ?>
<?php $this->print_list_count( '<div class="%s"><span class="list-display-count">' ) ?>
</caption>
<?php else : ?>
<?php
/* print the count if enabled in the shortcode
*
* the tag wrapping the count statment can be supplied in the function argument, example here
*/
$this->print_list_count( '<caption class="%s" ><span class="list-display-count">' );
?>
<?php endif ?>
<?php if ( $record_count > 0 ) : // print only if there are records to show ?>
<?php if ( count( $header_row ) > 1 ) : ?>
<thead>
<tr>
<?php
foreach ( $header_row as $header )
{
echo '<th>' . $header . '</th>';
}
?>
</tr>
</thead>
<?php endif ?>
<tbody>
<?php
foreach ( $table_rows as $row )
{
echo '<tr>';
foreach ( $row as $i => $field_value )
{
if ( $i === 0 )
{
echo '<th>' . $field_value . '</th>';
} else
{
echo '<td>' . $field_value . '</td>';
}
}
echo '</tr>';
}
?>
</tbody>
<?php else : // if there are no records ?>
<tbody>
<tr>
<td><?php if ( $this->is_search_result ) echo Participants_Db::plugin_setting( 'no_records_message' ) ?></td>
</tr>
</tbody>
<?php endif; // $record_count > 0 ?>
</table>
<?php
/*
* this shortcut function presents a pagination control with default layout
*/
$this->show_pagination_control();
?>
</div>
@xnau
Copy link
Author

xnau commented Aug 15, 2022

You will probably want to modify the value of $header_column to match the name of the field you want to use as the header for each column.

The HTML this generates is bare bones, you may want to add classes, etc.

Read the linked page if you don't know how to use Participants Database Custom Templates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment