简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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