简体   繁体   English

在CakePHP中以字母顺序列出记录并带有标题

[英]List records in alphabetical order with headers in CakePHP

I'm displaying a list of topics for an app written in CakePHP like so: 我正在显示一个用CakePHP编写的应用程序的主题列表,如下所示:

<ul>
    <?php foreach ($topics as $topic): ?>
        <li>
            <?php echo $topic['Topic']['title']; ?>
        </li>
        <?php endforeach; ?>
</ul>

What I'd like to do is display them like: 我想做的就是显示它们:

<h3>A</h3>
<ul>
    <li>animals</li>
    <li>anger</li>
    <li>age</li>
</ul>

<h3>B</h3>
<ul>
    <li>bat</li>
    <li>bird</li>
</ul>

etc...

How would I do that? 我该怎么做? Bearing in mind that I could also have numbers eg 3d 请记住,我也可以有数字,例如3d

ARRAY 阵列

array(
    (int) 0 => array(
        'Topic' => array(
            'id' => '1',
            'title' => 'awesome'
        ),
        'TopicPost' => array(
            (int) 0 => array(
                'id' => '1',
                'topic_id' => '1',
                'post_id' => '107'
            )
        )
    ),
    (int) 1 => array(
        'Topic' => array(
            'id' => '2',
            'title' => 'amazing'
        ),
        'TopicPost' => array(
            (int) 0 => array(
                'id' => '2',
                'topic_id' => '2',
                'post_id' => '107'
            )
        )
    ),
    (int) 2 => array(
        'Topic' => array(
            'id' => '3',
            'title' => 'jazz'
        ),
        'TopicPost' => array(
            (int) 0 => array(
                'id' => '3',
                'topic_id' => '3',
                'post_id' => '107'
            )
        )
    )
)

My php is a bit rusty, so I apologise for possible errors, but the logic is hopefully clear. 我的php有点生锈,因此我对可能的错误表示歉意,但逻辑很明确。 You can try this: 您可以尝试以下方法:

    <?php  
        $lastTopic = ' ';
        $first = true;
        $close = false;
        foreach ($topics as $topic): 
            $lastLetter = substr($lastTopic, 0, 1);
            $thisLetter = substr($topic['Topic']['title'], 0, 1);
            $isNewLetter =  $lastLetter == $thisLetter;
            if ($isNewLetter):
                $close = true;
                if($first) $first = false;
                else echo "</ul>";
    ?>
                <h3><?php echo $newLetter ?></h3>
                <ul>
                <?php endif; ?>
                    <li>
                        <?php 
                            echo $topic['Topic']['title']; 
                            $lastTopic = $topic['Topic']['title']; 
                        ?>
                    </li>

         <?php endforeach; ?>
         <?php if($close) echo "</ul>"; ?>
</ul>

The basic idea is that every time you encounter a topic starting with a new letter you close the html list and create a new header. 基本思想是,每次遇到以新字母开头的主题时,您都会关闭html列表并创建一个新标题。 This of course assumes that the list is already sorted. 当然,这假定列表已经排序。

What you'll need to do is first create an array of sorted items which you can foreach through. 您需要做的是首先创建一个可以对foreach项目进行排序的已排序项目数组。 When you're building this array take the first letter of each topic and create a list of topics with the same letter or number. 构建此数组时,请获取每个主题的首字母,并创建具有相同字母或数字的主题列表。

Given that $topic equals this: 假设$topic等于:

$topic = array(
    (int) 0 => array(
        'Topic' => array(
            'id' => '1',
            'title' => 'awesome'
        ),
        'TopicPost' => array(
            (int) 0 => array(
                'id' => '1',
                'topic_id' => '1',
                'post_id' => '107'
            )
        )
    ),
    (int) 1 => array(
        'Topic' => array(
            'id' => '2',
            'title' => 'amazing'
        ),
        'TopicPost' => array(
            (int) 0 => array(
                'id' => '2',
                'topic_id' => '2',
                'post_id' => '107'
            )
        )
    ),
    (int) 2 => array(
        'Topic' => array(
            'id' => '3',
            'title' => 'jazz'
        ),
        'TopicPost' => array(
            (int) 0 => array(
                'id' => '3',
                'topic_id' => '3',
                'post_id' => '107'
            )
        )
    )
);

You can use the following code to process the array: 您可以使用以下代码来处理数组:

<?
foreach($topic as $currentTopic):
    $thisLetter = strtoupper($currentTopic['Topic']['title'][0]);
    $sorted[$thisLetter][] = $currentTopic['Topic']['title'];
    unset($thisLetter);
endforeach;

foreach($sorted as $key=>$value):

    echo '<h3>'.$key.'</h3>';
    echo '<ul>';

    foreach($value as $thisTopic):
        echo '<li>'.$thisTopic.'</li>';
    endforeach;

    echo '</ul>';

endforeach;
?>

This has been tested successfully. 已成功测试。

Try these codes: 尝试以下代码:

<ul style="list-style-type:upper-alpha">
  <?php foreach ($topics as $topic): ?>
    <li>
        <?php echo $topic['Topic']['title']; ?>
    </li>
    <?php endforeach; ?>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM