cityModel.php 2.7 KB

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