cate.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. class cate_m extends Model
  3. {
  4. var $langid = "zh";
  5. function __construct()
  6. {
  7. parent::Model();
  8. }
  9. function cate_m()
  10. {
  11. $this->__construct();
  12. }
  13. function set_langid($langid="zh")
  14. {
  15. $this->langid = $langid;
  16. }
  17. //取得第一个分类信息
  18. function get_one($id,$condition="")
  19. {
  20. $sql = "SELECT c.*,m.title,m.if_cate,m.if_list,m.if_msg FROM ".$this->db->prefix."cate c JOIN ".$this->db->prefix."module m ON(c.module_id=m.id) WHERE c.id='".$id."'";
  21. if($condition)
  22. {
  23. $sql.= " AND ".$condition;
  24. }
  25. return $this->db->get_one($sql);
  26. }
  27. function get_cid_from_code($code)
  28. {
  29. $sql = "SELECT id FROM ".$this->db->prefix."cate WHERE identifier='".$code."' AND langid='".$this->langid."'";
  30. $rs = $this->db->get_one($sql);
  31. if(!$rs)
  32. {
  33. return false;
  34. }
  35. return $rs["id"];
  36. }
  37. //取得父级分类
  38. function get_parent_array(&$array,$id=0)
  39. {
  40. if(!$id)
  41. {
  42. return $array;
  43. }
  44. else
  45. {
  46. $rs = $this->get_one($id);
  47. if(!$rs)
  48. {
  49. return $array;
  50. }
  51. $array[] = $rs;
  52. if($rs["parentid"])
  53. {
  54. $this->get_parent_array($array,$rs["parentid"]);
  55. }
  56. else
  57. {
  58. return $array;
  59. }
  60. }
  61. }
  62. //取得子分类ID号
  63. function get_sonid_array(&$array,$id=0)
  64. {
  65. if(!$id)
  66. {
  67. return $array;
  68. }
  69. $sql = "SELECT id FROM ".$this->db->prefix."cate WHERE parentid='".$id."' AND status='1'";
  70. $rslist = $this->db->get_all($sql);
  71. if(!$rslist)
  72. {
  73. return $array;
  74. }
  75. foreach($rslist AS $key=>$value)
  76. {
  77. $array[] = $value["id"];
  78. $this->get_sonid_array($array,$value["id"]);
  79. }
  80. return $array;
  81. }
  82. //取得第一个主题
  83. function get_cate2sub($idstring,$ordertype="")
  84. {
  85. $sql = "SELECT id,identifier FROM ".$this->db->prefix."list WHERE status='1' AND cate_id IN(".$idstring.") ORDER BY istop DESC,taxis DESC ";
  86. if($ordertype)
  87. {
  88. $sql .= ", ";
  89. $sql .= str_replace(":"," ",$ordertype);
  90. }
  91. $sql.= ",id DESC LIMIT 1";
  92. return $this->db->get_one($sql);
  93. }
  94. function get_id_from_module($mid)
  95. {
  96. $sql = "SELECT id,identifier FROM ".$this->db->prefix."list WHERE status='1' AND module_id='".$mid."' ORDER BY istop DESC,taxis DESC ";
  97. $sql.= "post_date DESC,id DESC LIMIT 1";
  98. return $this->db->get_one($sql);
  99. }
  100. //取得所有模块中的标识串及ID,并配上对应关系
  101. function get_id_code_list()
  102. {
  103. $sql = "SELECT id,identifier FROM ".$this->db->prefix."cate WHERE langid='".$this->langid."'";
  104. $rslist = $this->db->get_all($sql);
  105. if(!$rslist)
  106. {
  107. return false;
  108. }
  109. $idlist = $codelist = array();
  110. foreach($rslist AS $key=>$value)
  111. {
  112. $idlist[$value["id"]] = $value["identifier"];
  113. $codelist[$value["identifier"]] = $value["id"];
  114. }
  115. return array("code"=>$codelist,"id"=>$idlist);
  116. }
  117. function get_catelist($module_id=0,$condition="")
  118. {
  119. $sql = "SELECT c.* FROM ".$this->db->prefix."cate c JOIN ".$this->db->prefix."module m ON(c.module_id=m.id) WHERE c.status='1' ";
  120. $sql.= " AND c.langid='".$this->langid."' ";
  121. if($module_id)
  122. {
  123. $sql .= " AND c.module_id='".$module_id."' ";
  124. }
  125. if($condition)
  126. {
  127. $sql.= " AND ".$condition;
  128. }
  129. $sql .= " ORDER BY c.taxis ASC,c.module_id DESC,c.parentid ASC,c.id DESC ";
  130. $rslist = $this->db->get_all($sql);
  131. if(!$rslist)
  132. {
  133. return false;
  134. }
  135. foreach($rslist AS $key=>$value)
  136. {
  137. if(!$value["parentid"])
  138. {
  139. $this->rootlist[] = $value;
  140. }
  141. else
  142. {
  143. $this->sublist[] = $value;
  144. }
  145. }
  146. $this->catelist = $rslist;
  147. return true;
  148. }
  149. //放在List操作中应用到的html表单
  150. function html_select($select_id="cateid",$selected=0,$lang="",$ext="")
  151. {
  152. $select = "<select name='".$select_id."' id='".$ext.$select_id."'>";
  153. if($lang)
  154. {
  155. $select.= "<option value='0'>".$lang."</option>";
  156. }
  157. if($this->rootlist && is_array($this->rootlist) && count($this->rootlist)>0)
  158. {
  159. foreach($this->rootlist AS $key=>$value)
  160. {
  161. $select .= "<option value='".$value["id"]."'";
  162. if($selected == $value["id"])
  163. {
  164. $select.= " selected";
  165. }
  166. $select .= ">".$space.$value["cate_name"];
  167. $select .= "</option>";
  168. $this->_html_select($value["id"],$selected,1);
  169. $select.= $this->html_ext;
  170. }
  171. }
  172. $select .= "</select>";
  173. return $select;
  174. }
  175. //根据当前分类取得子类HTML
  176. function _html_select($parentid=0,$selected=0,$space_id=1)
  177. {
  178. if(!$this->catelist)
  179. {
  180. return false;
  181. }
  182. $space = "";
  183. for($i=0;$i<$space_id;$i++)
  184. {
  185. $space .= "  ";
  186. }
  187. foreach($this->catelist AS $key=>$value)
  188. {
  189. if($value["parentid"] == $parentid)
  190. {
  191. $this->html_ext .= "<option value='".$value["id"]."'";
  192. if($value["id"] == $selected)
  193. {
  194. $this->html_ext .= " selected";
  195. }
  196. $this->html_ext .= ">".$space.$value["cate_name"];
  197. $this->html_ext .= "</option>";
  198. $this->_html_select($value["id"],$selected,($space_id+1));
  199. }
  200. }
  201. return true;
  202. }
  203. function html_select_array()
  204. {
  205. $rslist = array();
  206. if(!$this->rootlist || !is_array($this->rootlist) || count($this->rootlist)<1)
  207. {
  208. return false;
  209. }
  210. foreach($this->rootlist AS $key=>$value)
  211. {
  212. $value["space"] = $space ? $space : "";
  213. $rslist[] = $value;
  214. $this->_html_select_array($rslist,$value["id"],1);
  215. }
  216. return $rslist;
  217. }
  218. function _html_select_array(&$rslist,$parentid=0,$space_id=1)
  219. {
  220. if(!$this->catelist)
  221. {
  222. return false;
  223. }
  224. $space = "";
  225. for($i=0;$i<$space_id;$i++)
  226. {
  227. $space .= "  ";
  228. }
  229. foreach($this->catelist AS $key=>$value)
  230. {
  231. if($value["parentid"] == $parentid)
  232. {
  233. $value["space"] = $space;
  234. $rslist[] = $value;
  235. $this->_html_select_array($rslist,$value["id"],($space_id+1));
  236. }
  237. }
  238. return true;
  239. }
  240. }
  241. ?>