使用数组/ if语句对表格数据进行字母表化

时间:2016-12-12 20:47:58

标签: php mysql

我有一些我正在循环的数据,并使用if语句进行字母顺序排列。我的代码有效,但问题是它似乎是一个非常“漫长的过程”。我想知道是否有另一种方法,我错过了可以使这更容易。

这是我的PHP:

    // MY QUERY
$query1 = "SELECT `categoryid`, `categoryname` 
            FROM `my_table_category` 
            ORDER BY `my_table_category`.`categoryname` ASC";

$browse = mysql_query($query1) or die(mysql_error());
$browse_rows = array();
while($row = mysql_fetch_assoc($browse)){
    $browse_rows[] = $row;
    }

// HERE MY ARRAYS FOR THE ALPHABET
    $list_a = array(); $list_b = array(); $list_c = array(); 
    $list_d = array(); $list_e = array(); $list_f = array(); 
    $list_g = array(); $list_h = array(); $list_i = array();
// etc...

// HERE IS WHERE I'M GRABBING THE CATEGORY NAMES BY THEIR FIRST LETTER 
// AND ADDING THEM TO AN ARRAY
foreach($browse_rows as $row){
    if($row['categoryname'][0] == 'A'){
        $list_a[] = $row['categoryname'];
    }elseif($row['categoryname'][0] == 'B'){
        $list_b[] = $row['categoryname'];
    }elseif($row['categoryname'][0] == 'C'){
        $list_c[] = $row['categoryname'];
    }elseif($row['categoryname'][0] == 'D'){
        $list_d[] = $row['categoryname'];
    }
} //etc...

这是我的HTML:

<!-- HERE IS HOW I DISPLAY MY DATA -->
 <div id="topics_a">
            <h2>A</h2>
            <ul class="browse_list">
            <?
            foreach($list_a as $name){
                if ($holdcat <> $name) {
                $holdcat = $name; ?>
                <li><a href="index.php?state="<? echo $template->State."#".$browse_row['categoryid'];?>><? echo $name; ?></a></li> 
            <? }} ?>
            </ul>
        </div>

        <div id="topics_b">
            <h2>B</h2>
            <ul class="browse_list">
            <?
            foreach($list_b as $name){
                if ($holdcat <> $name) {
                $holdcat = $name; ?>
                <li><a href="index.php?state="<? echo $template->State."#".$browse_row2['categoryid'];?>><? echo $name; ?></a></li> 
            <? }} ?>
            </ul>
        </div>
//etc...

答案后更新:

//UPDATED PHP
foreach ($browse_rows as $row) {
$initial = $row['categoryname'][0];
    $lists[$initial][] = $row['categoryname'];
}
//UPDATED HTML
<? foreach ($lists as $letter => $list){ ?>
            <div>
                <h2><? echo $letter;  ?></h2>
                <ul class="browse_list"> 
            <? 
            $list = array_unique($list); 
            foreach ($list as $cat) {
            ?>
                <li><a href="#"><? echo $cat; ?></a></li> 
            <? } ?>
            </ul>
        </div>
            <? } ?>

输出: http://d.pr/i/bt68

1 个答案:

答案 0 :(得分:1)

使用多维数组,而不是为每个字母分别使用数组。

$lists = array();
foreach ($browse_list as $row) {
    $initial = $row['categoryname'][0];
    if (!isset($row[$initial])) {
        $lists[$initial] = array();
    }
    $lists[$initial][] = $row['categoryname'];
}

然后,当您想要显示它时,按键对数组进行排序,然后使用嵌套循环:

ksort($lists);
foreach ($lists as $list) {
    echo '<ul class="browse_list">';
    $list = array_unique($list); // get rid of duplicates
    foreach ($list as $cat) {
        echo '<li><a href="#">' . $cat . '</a></li>';
    }
    echo '</ul>';
}