cityModel.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. //城市
  3. class cityClassModel extends Model
  4. {
  5. //获取城市路径
  6. public function getpath($id)
  7. {
  8. $this->pathss = array();
  9. $this->getpaths($id);
  10. return $this->pathss;
  11. }
  12. private function getpaths($id)
  13. {
  14. $rs = $this->getone($id);
  15. if($rs && $rs['pid']!=$id){
  16. $this->getpaths($rs['pid']);
  17. $this->pathss[] = $rs;
  18. }
  19. }
  20. /**
  21. * 导入数据
  22. */
  23. public function daorudata()
  24. {
  25. $barr = c('xinhuapi')->getdata('base','city');
  26. if(!$barr['success'])return returnerror($barr['msg']);
  27. $data = $barr['data'];
  28. $shul = 0;
  29. foreach($data as $k=>$rs){
  30. $id = $rs['id'];
  31. if($this->rows($id)==0){
  32. $shul++;
  33. $this->insert($rs);
  34. }
  35. }
  36. return returnsuccess('成功导入'.$shul.'条数据');
  37. }
  38. /**
  39. * 籍贯/城市数据
  40. */
  41. public function citydata()
  42. {
  43. $rows = $this->db->getall('SELECT a.name,b.name as name1 FROM `[Q]city` a left join `[Q]city` b on a.`pid`=b.`id` where a.`type` in(2)');
  44. $barr = array();
  45. foreach($rows as $k=>$rs){
  46. $name = $rs['name'];
  47. if($name!=$rs['name1'])$name=$rs['name1'].$name;
  48. $barr[] = array(
  49. 'name' => $name,
  50. 'cityname' => $rs['name'],
  51. 'shengname' => $rs['name1'],
  52. );
  53. }
  54. return $barr;
  55. }
  56. /**
  57. * 省份数据
  58. */
  59. public function shengdata()
  60. {
  61. $rows = $this->db->getall('SELECT `name` FROM `[Q]city` where `pid`=1 order by `sort`');
  62. $barr = array();
  63. foreach($rows as $k=>$rs){
  64. $name = $rs['name'];
  65. $barr[] = array(
  66. 'name' => $name
  67. );
  68. }
  69. return $barr;
  70. }
  71. /**
  72. * 全部城市数据,3级
  73. */
  74. public function alldata()
  75. {
  76. $chche = c('cache');
  77. $cdata = $chche->get('cityalldata');
  78. if(!$cdata){
  79. $this->drowsa = $this->getall('1=1','*','`sort`');
  80. $this->datass = array();
  81. $this->alldatas($this->drowsa, '1', 0,'');
  82. $chche->set('cityalldata', $this->datass);
  83. return $this->datass;
  84. }else{
  85. return $cdata;
  86. }
  87. }
  88. private function alldatas($rows, $pid, $xu, $sub, $sheng='', $city='')
  89. {
  90. $keya = array('shengname','cityname','xianname');
  91. $zxij = 0;
  92. foreach($rows as $k=>$rs){
  93. if($rs['pid']==$pid){
  94. $zxij++;
  95. $sar = array(
  96. 'name' => $sub.$rs['name'],
  97. 'value' => $rs['name'],
  98. 'padding'=> 30*$xu,
  99. 'shengname' => $sheng,
  100. 'cityname' => $city,
  101. 'xianname' => '',
  102. );
  103. $keysas = $keya[$xu];
  104. $sar[$keysas] = $rs['name'];
  105. $this->datass[] = $sar;
  106. unset($this->drowsa[$k]);
  107. if($xu==0)$sheng = $rs['name'];
  108. if($xu==1)$city = $rs['name'];
  109. $xiji = $this->alldatas($this->drowsa, $rs['id'], $xu+1, $sub.$rs['name'], $sheng, $city);
  110. }
  111. }
  112. return $zxij;
  113. }
  114. }
粤ICP备19079148号