0

我正在显示一个用 CakePHP 编写的应用程序的主题列表,如下所示:

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

我想做的是将它们显示为:

<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...

我该怎么做?请记住,我也可以有数字,例如 3d

大批

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'
            )
        )
    )
)
4

3 回答 3

0

我的 php 有点生疏,所以我为可能的错误道歉,但希望逻辑清晰。你可以试试这个:

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

基本思想是每次遇到以新字母开头的主题时,您都会关闭 html 列表并创建一个新标题。这当然假设列表已经排序。

于 2012-04-15T23:20:59.493 回答
0

您需要做的是首先创建一个您可以foreach通过的排序项目数组。当您构建此数组时,获取每个主题的第一个字母并创建一个具有相同字母或数字的主题列表。

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

您可以使用以下代码来处理数组:

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

这已成功测试。

于 2012-04-15T23:34:00.903 回答
-1

试试这些代码:

<ul style="list-style-type:upper-alpha">
  <?php foreach ($topics as $topic): ?>
    <li>
        <?php echo $topic['Topic']['title']; ?>
    </li>
    <?php endforeach; ?>
</ul>
于 2012-04-15T22:59:45.587 回答