I'm using a database in MySQL and returning values via PHP. I'm using the framework Zend2.
I have the following table:
[ID] | [Name] | [Amount]
-----|--------|---------
1 | Alex | 10
-----|--------|---------
2 | Bruno | 5
-----|--------|---------
3 | Miguel | 6
-----|--------|---------
4 | Ana | 5
I need to unite draws. In this case, instead of displaying
the result should display
Code to show in HTML:
<?php foreach ($rows as $row) : ?>
<tr>
<td><?php echo ++$i; ?></td>
<td><?php echo $this->escapeHtml($row->amount); ?></td>
<td><?php echo $this->escapeHtml($row->name); ?></td>
<td><img src="<?php echo $row->imageMedal($i); ?>"/></td>
</tr>
<?php endforeach; ?>
The Zend2 structure has not changed.
You can GROUP_BY
amount and concatenate Name
in the groups with the same amount
, then you can order by amount
from the greatest value and cut the first three rows :
SELECT `amount`
, GROUP_CONCAT(`Name` SEPARATOR ', ') AS `name`
FROM `table`
GROUP BY `amount`
ORDER BY `amount` DESC
LIMIT 0, 3
@KeplerBR, here's a solution in ZF2 for your query: https://gist.github.com/settermjd/3360ccabb2b761a6ee11