YourController extends AbstractActionController
{
protected $tableGateway;
...
public function sortAction()
{
// contains the db field, which should the result sorted by or an empty string
$sort = $this->params()->fromQuery('sort', '');
// contains the sort direction the db query should ordered by (asc or desc)
$direction = $this->params()->fromQuery('order', '');
// executes sql query with the given parameters
$result = $this->tableGateway->getSortedResult($sort, $direction);
// return result to the view
return new ViewModel([
'result' => $result,
]);
}
...
}
getSortedResult
$sort
$direction
class YourTableGateway extends TableGateway
{
...
public function getSortedResult(string $sort, string $direction) : ResultSet
{
$select = $this->getSql()->select();
$select->columns([
'column_a',
'column_b',
'column_c',
]);
$select->order($sort . ' ' . $direction);
$resultset = $this->selectWith($select);
return $restulset;
}
...
}
<thead>
<tr>
<th><a href="<?= $this->url('name', [ 'action' => 'show', 'id' => 1 ], [ 'query' => [ 'sort' => 'name', 'order' => 'ASC' ]]); ?>">Table Headline 1</a></th>
<th><a href="<?= $this->url('name', [ 'action' => 'show', 'id' => 1 ], [ 'query' => [ 'sort' => 'gender', 'order' => 'ASC' ]]); ?>">Table Headline 2</a></th>
</tr>
</thead>
sort
order
query