我现在有这个代码来回显我的所有数据库条目,我想知道如果db条目的第一个值是a,它在ZF中回显条目是什么样的。
代码:
<table>
<?php foreach($this->clubs as $clubs) : ?>
<tr>
<td><a href="<?php echo $this->url(array('controller' => 'club-description', 'action' => 'index', 'club_id' => $clubs->id));?>">
<?php echo $this->escape($clubs->club_name);?></a></td>
<td><?php echo $this->escape($clubs->rating);?></td>
</tr>
<?php endforeach; ?>
</table>
由于
里克多维
答案 0 :(得分:0)
因此,根据您的评论,您可能希望在页面上按字母对俱乐部进行分组。假设它们在数据库查询中按字母顺序排序,最简单的方法是保留一个变量,该变量存储循环中最后一个俱乐部的第一个字母。然后,在每次迭代中,您将当前俱乐部的第一个字母与前一个俱乐部的第一个字母进行比较。如果它们不同,则输出新标题。
使用您的代码,这看起来像这样:
<?php
$previousLetter = false;
?>
<table>
<?php foreach($this->clubs as $clubs) : ?>
<?php
$firstLetter = substr($clubs->_club_name, 0, 1);
if ($firstLetter != $previousLetter) {
?>
<tr>
<td><?php echo $firstLetter; ?></td>
</tr>
<?php } ?>
<tr>
<td><a href="<?php echo $this->url(array('controller' => 'club-description', 'action' => 'index', 'club_id' => $clubs->id));?>">
<?php echo $this->escape($clubs->club_name);?></a></td>
<td><?php echo $this->escape($clubs->rating);?></td>
</tr>
<?php $previousLetter = $firstLetter; ?>
<?php endforeach; ?>
</table>