分类按照拼音第一字母排序显示实现

本文介绍了一种从品牌名称中提取首字母的方法,并通过查询系统获取品牌信息,然后将这些信息按照首字母进行整理和展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先写一个提取品牌第一字母的函数 其实 这是最重要的
  1. $dict=array(

  2. 'a'=>0xB0C4,

  3. 'b'=>0xB2C0,

  4. 'c'=>0xB4ED,

  5. 'd'=>0xB6E9,

  6. 'e'=>0xB7A1,

  7. 'f'=>0xB8C0,

  8. 'g'=>0xB9FD,

  9. 'h'=>0xBBF6,

  10. 'j'=>0xBFA5,

  11. 'k'=>0xC0AB,

  12. 'l'=>0xC2E7,

  13. 'm'=>0xC4C2,

  14. 'n'=>0xC5B5,

  15. 'o'=>0xC5BD,

  16. 'p'=>0xC6D9,

  17. 'q'=>0xC8BA,

  18. 'r'=>0xC8F5,

  19. 's'=>0xCBF9,

  20. 't'=>0xCDD9,

  21. 'w'=>0xCEF3,

  22. 'x'=>0xD188,

  23. 'y'=>0xD4D0,

  24. 'z'=>0xD7F9,

  25. );

  26. //取GB2312字符串首字母,原理是GBK汉字是按拼音顺序编码的.

  27. function get_letter($input)

  28. {
  29. global $dict;

  30. $str_1 = substr($input, 0, 1);

  31. if ($str_1 >= chr(0x81) && $str_1 <= chr(0xfe)) {

  32. $num = hexdec(bin2hex(substr($input, 0, 2)));

  33. foreach ($dict as $k=>$v){

  34. if($v>=$num)

  35. break;

  36. }

  37. return $k;

  38. }

  39. else{

  40. return $str_1;

  41. }
  42. }
复制代码
第二步 结合我们的系统进行查询

  1.     $sql = 'SELECT * FROM ' . $GLOBALS['ecs']->table('brand').' order by brand_name';
  2.     $brand_array = $GLOBALS['db']->getall($sql);
  3. $brand_list = array();
  4. for($i=0;$i<count($brand_array);$i++)
  5. {
  6.    $brand_list[get_letter($brand_array[$i]['brand_name'])][$brand_array[$i]['brand_id']] = $brand_array[$i]['brand_name']."-".$brand_array[$i]['brand_id'];
  7.    
  8. }
  9. 得到一个以第一字母为主键的数组
复制代码
第三步 显示


  1. foreach ($brand_list AS $row=>$idx)
  2. {
  3.   
  4.   $show .="<table width=\"766\" height=\"103\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
  5.   <tr>
  6.     <td align=\"center\" style=\"border-right:1px solid #D0DFE8;border-bottom:1px solid #D0DFE8;border-left:1px solid #D0DFE8\"><table width=\"97%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  7.       <tr>
  8.         <td ><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  9.           <tr>
  10.             <td width=\"90%\" style=\"font-size:18px\"><strong><font color=\"#DC0000\">".$row."</font><a name=".$row." id=".$row."></a></strong></td>
  11.               <td style=\"font-size:14px\"><strong><a href=\"#top\" style=\"color: #0073CF;text-deocration:none\">Top</a></strong></td>
  12.             </tr>
  13.           </table></td>
  14.         </tr>
  15.       <tr>
  16.     <td style=\"line-height:200%\">
  17.   <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  18.           <tr>";
  19.     $a = 0;
  20.   foreach($idx AS $row2=>$idx2)
  21.    {
  22.     $a++;
  23.     $idx2 = explode('-',$idx2);
  24.      $show .= "<td  align=\"left\"><span style=\"font-size:12px;\"><a href='brand.php?id=$idx2[1]' style=\"color:#00509f;text-decoration:none\">$idx2[0]</a></span></td>";
  25.   if($a%6 == 0)
  26.   {
  27.    $show .="</tr><tr>";
  28.   }
  29. }
  30.    $show .="</tr>
  31.         </table>
  32.   </td>
  33.         </tr>
  34.     </table>
  35. </td>
  36.   </tr>
  37. </table>";
  38. }
  39. $smarty->assign("show",$show);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值