如何让ecshop按分类首字母排序的方法 我们要实现的是 把分类按拼音的首个字母来分类展示 首先我们在分类主表价格字段!这个字段的用处就是用来保存每个分类的首字母 后台执行SQL

如何让ecshop分类首字母排序的方法

我们要实现的是 把分类按拼音的首个字母来分类展示
首先我们在分类主表价格字段!这个字段的用处就是用来保存每个分类的首字母
后台执行SQL
输入以下代码

ALTER TABLE `ecs_category` ADD `cat_name_zm` VARCHAR( 10 ) NOT NULL AFTER `cat_name` ;

复制代码

打开编辑分类的程序 在admin 目录下的 category.php
找到

if ($_REQUEST['act'] == ‘insert’)

复制代码

在上面添加

/*拼音函数–start*/
function getinitial($str)
{
$asc=ord(substr($str,0,1));
if ($asc<160) //非中文
{
if ($asc>=48 && $asc<=57){
return ’1′; //数字
}elseif ($asc>=65 && $asc<=90){
return chr($asc); // A–Z
}elseif ($asc>=97 && $asc<=122){
return chr($asc-32); // a–z
}else{
return ‘~’; //其他
}
}
else //中文
{
$asc=$asc*1000+ord(substr($str,1,1));
//获取拼音首字母A–Z
if ($asc>=176161 && $asc<176197){
return ‘A’;
}elseif ($asc>=176197 && $asc<178193){
return ‘B’;
}elseif ($asc>=178193 && $asc<180238){
return ‘C’;
}elseif ($asc>=180238 && $asc<182234){
return ‘D’;
}elseif ($asc>=182234 && $asc<183162){
return ‘E’;
}elseif ($asc>=183162 && $asc<184193){
return ‘F’;
}elseif ($asc>=184193 && $asc<185254){
return ‘G’;
}elseif ($asc>=185254 && $asc<187247){
return ‘H’;
}elseif ($asc>=187247 && $asc<**66){
return ‘J’;
}elseif ($asc>=**66 && $asc<192172){
return ‘K’;
}elseif ($asc>=192172 && $asc<194232){
return ‘L’;
}elseif ($asc>=194232 && $asc<196195){
return ‘M’;
}elseif ($asc>=196195 && $asc<197182){
return ‘N’;
}elseif ($asc>=197182 && $asc<197190){
return ‘O’;
}elseif ($asc>=197190 && $asc<198218){
return ‘P’;
}elseif ($asc>=198218 && $asc<200187){
return ‘Q’;
}elseif ($asc>=200187 && $asc<200246){
return ‘R’;
}elseif ($asc>=200246 && $asc<203250){
return ‘S’;
}elseif ($asc>=203250 && $asc<205218){
return ‘T’;
}elseif ($asc>=205218 && $asc<206244){
return ‘W’;
}elseif ($asc>=206244 && $asc<209185){
return ‘X’;
}elseif ($asc>=209185 && $asc<212209){
return ‘Y’;
}elseif ($asc>=212209){
return ‘Z’;
}else{
return ‘~’;
}
}
}
/*拼音函数–end*/

复制代码

找到

$cat['cat_id'] = !empty($_POST['cat_id']) ? intval($_POST['cat_id']) : 0;

复制代码

在上面添加

*设置分类首字母*/
$zmcat_name = $_POST['cat_name'];
$cat['cat_name_zm'] = getinitial($zmcat_name);

复制代码

前台调用

/*查询所有分类*/
$cat_sql = “select * from ecs_category ORDER BY cat_name_zm ASC”;
$res = $GLOBALS['db']->getAll($cat_sql);
$smarty->assign(‘categorycat’,$res);

复制代码

模板判断输出

<!–{foreach from=$categorycat item=cat}–>
{if $cat.cat_name_zm eq A}
<li><a href=”category.php?id={$cat.cat_id}”>{$cat.cat_name} </a></li>
{/if}
<!–{/foreach}–>

复制代码

简简单的方法吧!直接在模板判断 {if $cat.cat_name_zm eq A} 这个意思是把分类首字母是 A 全部输出来!
转载请说明出处
知优网 » 如何让ecshop按分类首字母排序的方法(ecshop首页楼层排序)

发表评论

您需要后才能发表评论