0

我有一个从数据库中获取的俱乐部列表,然后使用以下代码对它们进行字母排序,并在它们上方添加第一个字母的标题。

<?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; 
?>

问题是当我在数据库中添加一个新条目时,它没有按字母顺序排列,它被添加到列表的末尾。

4

1 回答 1

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');
于 2012-05-03T07:29:52.347 回答