我有一个从数据库中获取的俱乐部列表然后使用以下代码对它们进行分类并添加上面第一个字母的标题。
<?php
$previousLetter = false;
?>
<?php
$i=1; // have a counter variable
foreach($this->clubs as $clubs) : ?>
<?php
$firstLetter = substr($clubs->club_name, 0, 1);
if ($firstLetter != $previousLetter) {
if($i==1){
echo "<div class='left_class'>"; // open left div
}
?>
<h3 id="club-link-header"><u><?php echo $firstLetter; ?></u></h3>
<?php } ?>
<a id="club-link" href="<?php echo $this->url(array('controller' => 'club-description', 'action' => 'index', 'club_id' => $clubs->id));?>"><br />
<?php echo $this->escape($clubs->club_name);?></a>
<?php $previousLetter = $firstLetter; ?>
<?php
if($i==25){
echo "</div>"; //close left div
echo "<div class='right_class'>"; // open right div
}
if($i==50){
echo "</div>"; //close right div
}
$i++; // increment the counter variable for each loop
endforeach;
?>
问题是当我向数据库中添加一个新条目时,它不是字母表,而是添加到列表的末尾。
答案 0 :(得分:1)
$this->view->clubs = $clubs->fetchAll(); // Fetches the clubs ordered by ID
$select = $clubs->select(); // Does nothing
$select->order('club_name ASC'); // Does nothing
您可以这样做:
$this->view->clubs = $clubs->fetchAll(null, 'club_name ASC');