简体   繁体   English

带标题的PHP动态字母列表

[英]PHP dynamic alphabetical list with headers

So what I am trying to produce is something like this: 所以我想生产的是这样的: 在此处输入图片说明

Except, I do not want duplicates, when there is more than one sign, it becomes duplicated as well as re creating the "C" list view header. 除非我不想重复,否则当有多个符号时,它就会重复,并重新创建“ C”列表视图标题。 My SQL query is creating a column known as first_char which is holding the first character of one of the other columns.......below is my code: 我的SQL查询正在创建称为first_char的列,该列保存其他列之一的第一个字符。......下面是我的代码:

<?php foreach ($data as $name=>$signs): ?>
    <!-- If name is not empty, create the header -->
        <?php if (isset($name) && !empty($signs)): ?> 
            <div data-role='collapsible-set' data-theme='d'>

                <h2>Username:&nbsp;<strong id="headerName"><?=$name?></strong></h2>

        <?php endif; ?>

    <?php endforeach; ?>
    <!-- for every sign, take the first char and create a list view -->
    <?php foreach ($signs as $char):
            $current_char = '';
            if ($char['first_char'] != $current_char){
                $current_char = $char['first_char'];
                echo '<ul data-role="listview" data-dividertheme="d" style="margin-top: 0;"><li data-role="list-divider">'.$current_char.'</li></ul><br>';
            }

            foreach ($signs as $sign):
                if($current_char == $sign['first_char'] and !empty($signs)):                           
    ?>

                    <div data-role='collapsible' data-collapsed='true' data-icon='arrow-l'>
                        <h3><?=$sign['sign_name']?></h3>
                        <div class='ui-grid-a'>                 
                            <div class="ui-block-a">
                                <div class="ui-bar ui-bar-d" style="height:25px;">
                                    <strong>Last Seen:</strong><strong id="text"><?=$sign['last_connected']?></strong>
                                </div>
                            </div>
                            <div class="ui-block-a">
                                <div class="ui-bar ui-bar-d" style="height:25px;">
                                    <strong>Resolution:</strong><strong id="text"><?=$sign['resolution_x'].'x'.$sign['resolution_y']?></strong>
                                </div>
                            </div>
                            <div class="ui-block-a">
                                <div class="ui-bar ui-bar-d" style="height:25px;">
                                    <strong>Group Name:</strong><strong id="text"><?=$sign['group_name']?></strong>
                                </div>
                            </div>
                            <div class="ui-block-a">
                                <div class="ui-bar ui-bar-d" style="height:25px;">
                                    <strong>Sign Number:</strong><strong id="text"><?=$sign['sign_id']?></strong>
                                </div>
                            </div>
                       </div>
                    </div>
            <?php endif; ?>
        <?php endforeach; ?>
    <?php endforeach; ?>

Thanks in advance 提前致谢

This fixes the previous error and gets rid of the duplication. 这可以修复先前的错误并消除重复。 $current_char needed to be outside the foreach loop, and removed the 2nd foreach. $ current_char需要在foreach循环之外,并删除了第二个foreach。

<?php $current_char = '';?>
        <?php foreach ($signs as $sign):?>
                <?php                       
                    if ($sign['first_char'] != $current_char){
                        $current_char = $sign['first_char'];
                        echo '<ul data-role="listview" data-dividertheme="b" style="margin-top: 0;padding-top: 3px;"><li data-role="list-divider">'.$current_char.'</li></ul><br>';                     
                    }
                ?>

The query used above for SQL was this: 上面用于SQL的查询是这样的:

SELECT 
    user.username,
    groups.group_id, groups.group_name,  
    sign.last_connected, sign.sign_id, sign.sign_name, sign.resolution_x, sign.resolution_y, LEFT(sign.sign_name, 1) AS first_char
FROM
    user
LEFT JOIN(
    groups, sign
)ON(
    user.user_id = groups.userID AND
    groups.group_id = sign.groupID
)
WHERE
    username = ? AND
    UPPER(sign.sign_name) BETWEEN "A" AND "Z"
    OR sign.sign_name BETWEEN "0" AND "9" ORDER BY sign.sign_name

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

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