【问题标题】:select & option, adding index alphabetically in php选择&选项,在php中按字母顺序添加索引
【发布时间】:2013-10-08 14:08:15
【问题描述】:

我正在使用 wordpress 开发一个网站。 我已经包含了一个选择类型菜单,其中包含一个父页面的子页面的名称和链接。 一切正常,我的页面按字母 ASC 排序:

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

这是我的代码:

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

我想知道是否可以像这样在我的菜单中自动添加字母索引(猜测宽度)。在所有以 A 开头的页面之前添加“A”(当有以 A 开头的页面时……),在所有以 B 开头的页面之前添加“B”,依此类推…… 必须看起来像这样的东西:

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

如果有人知道我能做到这一点,将不胜感激! 非常感谢

马蒂厄

【问题讨论】:

    标签: php html wordpress select optgroup


    【解决方案1】:

    如下使用(仅供演示):

        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>';
    }
    

    虽然没有检查..如果您遇到任何问题,请检查并返回


    如果您需要显示所有字母,我可以给您一个建议

    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>';
        }
    }
    

    【讨论】:

    • 您好!多谢 !这就是我一直在尝试做的,太好了......只有一个问题,它生成了 ,在我生成的源代码中我可以看到 B,但我的菜单中没有显示任何内容,只有一个空行...当涉及到两个词的城市时,例如旧金山,我有这个 SF,而不是 S...所以我非常接近结果!!!这是我的代码,包括您的代码:gist.github.com/mattieumoreau/6790846,非常感谢您的帮助
    • 好吧,我通过替换 echo ''.$initial.'' 解决了显示问题;通过 echo ''; 但我怎样才能只显示第一个单词@SKV 的第一个字母?谢谢!-
    • 感谢您的帮助@SKV,您给我的代码运行良好(我刚刚添加以显示这封信)。但我有一个问题,它显示我的字符串的每个单词的第一个字母......例如:对于旧金山,它显示 SF,而不是“S”gist.github.com/mattieumoreau/6791683,即使我没有任何页面也有可能从 D 开始,让“D”显示在 optgroup 中?我们的想法是显示所有 26 个字母...再次感谢您的帮助!
    • 请再次检查我的答案,我已经编辑了它。请注意它的演示想法。
    • 谢谢!抱歉,我没有看到您的答案...我添加了代码并检查了错误,但找不到任何错误,但是它不起作用,而是出现了空白页...我一定做错了什么? ? gist.github.com/mattieumoreau/6791846
    【解决方案2】:

    您好,我已根据您的需要再次编辑了代码。

    请检查

    <?php
    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));
            break;
        }
        return $inits; 
    }
    $currval = "";
    $alp = array('A','B','C','D','E','Y','S','Z');
    //$children = array('Asin','Bali','Bruxelles','Chicago','Denmark','Edinburgh','Flag','San Francisco');
    ?>
    <select name="test">
    <?php
    foreach($alp as $key => $abc) {
        $showinOpt = false;
        foreach($children as $child) {
          $permalink = get_permalink($child->ID);
          $initial = getInitials($child->post_title);
           if($initial == $abc) {
               if($currval != $initial) {
                    $currval = $initial;
                    echo '<optgroup label="'.$initial.'">'.$initial.'</optgroup>';
                    $showinOpt = true;
               }
               echo '<option value="'.$permalink.'">'.$child->post_title.'</option>';
            }
        }
        if(!$showinOpt) {
            echo '<optgroup label="'.$abc.'">'.$abc.'</optgroup>';
        }
    }
    ?>
    </select>
    

    在你得到 $children 数组后添加它。

    【讨论】:

    • 感谢您的回答,但是使用这种方法我没有在我的函数中获取子页面 ID...我想我需要在这里添加一些东西 $array1 = array('Asin','巴厘岛','布鲁塞尔','芝加哥','丹麦','爱丁堡','国旗');我需要使用 $permalink = get_permalink($child->ID); 构建我的数组$initial = getInitials($child->post_title);你知道我的意思吗?
    • 是的,我明白,但我需要将我的子页面放入这个数组 $array1 = array('Asin','Bali','Bruxelles','Chicago','Denmark','Edinburgh ','旗帜'); ...它必须是动态的,你明白吗?
    • 是的,它工作得很好!我在我的 php 编码中犯了一个错误,但现在一切正常!非常感谢您的帮助!
    • 我很高兴,虽然我也希望得到支持 + 接受,没关系,不用担心
    • 很高兴,但我该怎么做呢?我是这个论坛的新手!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    相关资源
    最近更新 更多