2012-05-02 134 views
0

我有一个从数据库中提取的俱乐部列表,然后使用下面的代码将它们按字母顺序排列,并在它们上面添加第一个字母的标题。Zend Framework按字母顺序排列的列表,新条目没有按字母顺序排列

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

的问题是,当我添加一个新进入它不是alphabetised数据库,它被添加到列表的末尾。

+0

只需添加一个'ORDER BY'子句到SQL查询。 – dbrumann

+0

我试过了,它没有工作,它会工作,如果我没有格式的列表我有..这里是控制器,我初始化ORDER BY - > http://pastebin.com/XrPMEQD0 – Rex89

回答

1

问题在于how you fetch the data

$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');