dongmi5177 2013-10-02 08:18
浏览 21
已采纳

select&option,在php中按字母顺序添加索引

I'm working on a website bilt with wordpress. I've included a select type menu with the name and links of my children pages of one parent page. everything works fine, my pages are sorted alphabetically ASC :

Bali
Bruxelles
Chicago
Edinburgh
Geneve
Los Angeles
Lyon
Miami
Paris
San Diego
San Francisco
Vancouver
Washington
Zurich

here is my code :

<div class="styled-select">
<?php

if(!$post->post_parent){

    $children = get_pages(array(
        'child_of' => $post->ID,
        'post_type' => 'page',
        'post_status' => 'publish',
        'sort_order' => 'ASC',
        'sort_column' => 'post_title',
    ));

}else{

    $children = get_pages(array(
        'child_of' => $post->post_parent,
        'post_type' => 'page',
        'post_status' => 'publish',
        'sort_order' => 'ASC',
        'sort_column' => 'post_title',
    ));
}

if ($children) {
    echo '<select name="" onchange="location = this.options[this.selectedIndex].value;">';
    echo '<option>'. 'A - Z' .'</option>';

    foreach($children as $child){
        //print_r($child);
        $permalink = get_permalink($child->ID);

        echo '<option value="'.$permalink.'">'.$child->post_title.'</option>';
    }
    echo '</select>';

} ?>

</div>

I wanted to know if its possible to add automatically an alphabet index (width in guess) inside my menu, like this. add "A" in before all pages beginning with A (when there are pages beginning with A...), add "B" in before all pages beginning with B, and so on... Something who must look like this :

A
B
  Bali
  Bruxelles
C
  Chicago
D
E
F 
  Edinburgh
G
  Geneve
H
I
J
K
L
  Los Angeles
  Lyon
M
  Miami
N
O
P
  Paris
Q
R
S
  San Diego
  San Francisco
T
U
V
  Vancouver
W
  Washington
X
Y
Z
  Zurich

If anybody know who I can do it it would be much appreciate ! thanks a lot

Mattieu

  • 写回答

2条回答 默认 最新

  • doutuoji8418 2013-10-02 08:27
    关注

    use as follows (for demonstration only) :

        function getInitials($name){
    //split name using spaces
    $words=explode(" ",$name);
    $inits='';
    //loop through array extracting initial letters
        foreach($words as $word){
        $inits.=strtoupper(substr($word,0,1));
        }
    return $inits;  
    }
    $currval = "";
    foreach($children as $child){
        //print_r($child);
        $permalink = get_permalink($child->ID);
        $initial = getInitials($child->post_title);
        if($initial!='' && $currval != $initial) {
            $currval = $initial;
            echo '<optgroup>'.$initial.'</option>';
        }
        echo '<option value="'.$permalink.'">'.$child->post_title.'</option>';
    }
    

    Not checked though.. please check and return if you face any prob


    In the case you need to show all alphabets, i can give you an idea to do so

    function getInitials($name){
    //split name using spaces
    $words=explode(" ",$name);
    $inits='';
    //loop through array extracting initial letters
        foreach($words as $word){
        $inits.=strtoupper(substr($word,0,1));
        }
    return $inits; 
    }
    $currval = "";
    
    
    $alp = array('A'...'Z')
    
    foreach($alp as $abc) {
        foreach($children as $child){
            //print_r($child);
            $permalink = get_permalink($child->ID);
            $initial = getInitials($child->post_title);
            if($initial!='' && $currval != $initial && $initial == $abc) {
                $currval = $initial;
                echo '<optgroup>'.$initial.'</option>';
            }
            echo '<option value="'.$permalink.'">'.$child->post_title.'</option>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致