ECOS百科全书|高级搜索|RSS订阅|加入收藏|服务中心|联系我们欢迎您光临!

ECStore下实现品牌按拼音首字母索引

来源:清风创科 发布时间:2016-12-12 17:25 字体:【   点击

在一些品牌特别多的网站,以及英文品牌为主的网站,客户喜欢按英文字母 和 拼音首字母分组排序和索引,以下是在ecstore下的实现方法:


1、开发目录结构示意图:


2、lib/pinyin.php:

class b2c_pinyin{
     
    // 码表
    var $fp  = null;
    var $dat = 'pinyin.dat';
     
    function b2c_pinyin(){
        $this->dat = CUSTOM_CORE_DIR.'/b2c/lib/'.$this->dat;
        if (is_file($this->dat)) {
            $this->fp = fopen($this->dat, 'rb');
        }
    }
    /**
     * 转拼音
     *
     * @param string $str   汉字
     * @param bool $ucfirst 首字母大写
     * @param bool $polyphony 忽略多读音
     * @return string
     */
     function encode($str, $ucfirst=true, $polyphony=true) {
        $ret = '';
        $len = mb_strlen($str, 'UTF-8');
        for ($i = 0; $i < $len; $i++) {
            $py = $this->pinyin(mb_substr($str, $i, 1, 'UTF-8'));
            if ($ucfirst && strpos($py, ',') !== false) {
                $pys = explode(',', $py);
                $ret.= implode(',', array_map('ucfirst', ($polyphony ? array_slice($pys, 0, 1) : $pys)));
            } else {
                $ret.= $ucfirst ? ucfirst($py) : $py;
            }
        }
        return $ret;
    }
    /**
     * 汉字转十进制
     *
     * @param string $word
     * @return number
     */
     function char2dec($word) {
        $bins  = '';
        $chars = str_split($word);
        foreach($chars as $char) $bins.= decbin(ord($char));
        $bins = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/', '$1$2$3', $bins);
        return bindec($bins);
    }
    /**
     * 单个字转拼音
     *
     * @param string $char  汉字
     * @return string
     */
     function pinyin($char){
        if (strlen($char) == 3 && $this->fp) {
            $offset = $this->char2dec($char);
            // 判断 off 值
            if ($offset >= 0) {
                fseek($this->fp, ($offset - 19968) <<>fp, 16));            }        }        return $char;    }      function __destruct() {        if ($this->fp) {            fclose($this->fp);        }    }}


3、lib/pinyin.data


3、controller/site/brand.php

public function nlist(){
                
        //定义面包屑
        $this->path[] = array('title'=>app::get('b2c')->_('品牌索引'),'link'=>$this->gen_url(array('app'=>'b2c', 'ctl'=>'site_brand', 'act'=>'nlist','full'=>1)));
        $GLOBALS['runtime']['path'] = $this->path;
                
        //定义标题、关键词、描述等
        $title = $title['title']?$title['title']:app::get('b2c')->_('品牌索引');
        $this->title = $title;
        $this->keywords = $title;
        $this->description = $title;
                
                
        //取出全站所有的品牌
        $oGoods = $this->app->model('brand');
                
        //得到结果集
        $result = $oGoods->getList();
                
                
        //24 chinese may first
        $letters=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
        $this->pagedata['letters']=$letters;
                
        //调用lib里定义的方法
        $Objpinying= kernel::single('b2c_pinyin');
                
        //遍历取出所有品牌的首字母
        foreach($letters as $k=>$v){
            $tmpkey=$v;
            foreach($result as $kl=>$vv){
                $vv['brand_firstletter']=strtolower(substr($Objpinying->encode($vv['brand_name']),0,1));
                if($v==trim($vv['brand_firstletter']) || ($tmpkey=='0-9' && is_numeric($vv['brand_firstletter']))){
                    $aLetterCat[$tmpkey][]=$vv;
                    $tmphave++;
                }
            }
        }
        
        //将字母表转换成大写
        foreach ($pagedata['letters'] as $key => $value) {
            $pagedata['letters'][$key]=strtoupper($value);//大写
        }
        
        //将品牌首字母转换为大写
        foreach ($aLetterCat as $key => $value) {
            if($key!=='0-9') {
                $aLetterCat[strtoupper($key)] = $value;//大写
                unset($aLetterCat[$key]);//
            }
        }
                
        $imageDefault = app::get('image')->getConf('image.set');
        $this->pagedata['defaultImage'] = $imageDefault['S']['default_image'];
                
        //输出数据
        $this->pagedata['data'] = $aLetterCat;
                
        //引入视图层文件
        $this->page('site/brand/nlist.html');
    }


4、view/site/brand/nlist.html:



5、前台访问路径:

http://yoursite/index.php/brand-nlist.html



装修后效果如下图:


------分隔线--------

关键词:ecstore,品牌索引,拼音索引

转载请保留:http://www.hnqss.cn/html/jszx/ecstore/article-2628.html

------分隔线--------
尚未注册畅言帐号,请到后台注册