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

来源:互联网 发布:淘宝精品下载 编辑:程序博客网 时间:2024/05/02 00:00
首先写一个提取品牌第一字母的函数 其实这是最重要的
  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 bybrand_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><fontcolor=\"#DC0000\">".$row."</font><aname=".$row."id=".$row."></a></strong></td>
  11.           <tdstyle=\"font-size:14px\"><strong><ahref=\"#top\" style=\"color:#0073CF;text-deocration:none\">Top</a></strong></td>
  12.         </tr>
  13.        </table></td>
  14.       </tr>
  15.     <tr>
  16.    <tdstyle=\"line-height:200%\">
  17.   <tablewidth=\"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\"><spanstyle=\"font-size:12px;\"><ahref='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);



0 0
原创粉丝点击