只选择一个字母

时间:2012-02-09 11:59:33

标签: php arrays

下面的脚本向我展示了我所有商店的清单,并按字母顺序排序。现在我想放,例如$ _GET ['letter'] ==“A”;它只会显示以字母A开头的商店。

我该怎么做?

$stores = get_terms(APP_TAX_STORE, array('hide_empty' => 0, 'parent' => 0));
$list = '';
$groups = array();

// get ids of all hidden stores 
$hidden_stores_query = "SELECT ". $wpdb->prefix ."clpr_storesmeta.stores_id FROM ". $wpdb->prefix ."clpr_storesmeta WHERE ". $wpdb->prefix ."clpr_storesmeta.meta_key = 'clpr_store_active' AND ". $wpdb->prefix ."clpr_storesmeta.meta_value = 'no'";
$hidden_stores = $wpdb->get_col($hidden_stores_query);

if ($stores && is_array($stores) ) {

    foreach($stores as $store)
        $groups[strtoupper($store->name[0])][] = $store;
    if (!empty($groups)) :
        foreach($groups as $letter => $stores) {
      $old_list = $list;

      $letter_items = false;
            $list .= "\n\t" . '<h2 class="stores" id="' . apply_filters( 'the_title', $letter ) . '">' . apply_filters( 'the_title', $letter ) . '</h2>';
            $list .= "\n\t" . '<ul class="stores">';

            foreach($stores as $store) {
                if (!in_array($store->term_id, $hidden_stores)) {
                    $list .= "\n\t\t" . '<li><a href="' . get_term_link($store, APP_TAX_STORE) . '"><img src="/images/korting-winkels/'. ereg_replace("[^A-Za-z0-9]", "", strtolower(apply_filters('the_title', $store->name))).'.png" width="133" class="afbeelding" height="68" alt="' . apply_filters('the_title', $store->name). '" title="' . apply_filters('the_title', $store->name). '" /></a><br /></li>';
          $letter_items = true;
        }
            }   

            $list .= "\n\t" . '</ul>';

      if(!$letter_items)
        $list = $old_list;
        }

    endif;

} else {

    $list .= "\n\t" . '<p>' . __('Sorry, but no stores were found.', 'appthemes') .'</p>';

}

2 个答案:

答案 0 :(得分:2)

你最好把它放在你的查询中(这样你就不会抽出页面上不需要的数据),例如:

SELECT * FROM `table` WHERE `name` LIKE '$startLetter%'

我认为这是由get_terms函数处理的,但没有看到我真的无法提供更多细节。

答案 1 :(得分:0)

您可以从数据库中进行选择,或者如果您不想这样做,则可以这样做,

$list = array("abc","abd","xyz","aml");//assuming this is your array
foreach($list as $listValue){
    if(substr($listValue, 0 ,1) == "a"){ //instead of "a" put $_GET['letter']
        $finalarray[] = $listValue;
    }
}

//在$finalarray中,您将只拥有以($_GET['letter']

开头的数据