Discuz!官方免费开源建站系统

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

Discuz! X2的 地区数据 调用分析

[复制链接]
otherbank 发表于 2011-9-12 19:12:31 | 显示全部楼层 |阅读模式
本帖最后由 otherbank 于 2011-9-13 14:48 编辑

关于地区数据调用,Discuz! X2前台的话在个人信息个人设置里面有,后台的话有地区数据设置,后台操作用户时也有用的到调用地区数据。虽然总的说Discuz! X2的地区数据调用不是很多,但是地区调用是必不可少,是非常重要的。现在分析一下 地区数据的调用方法。
以前台 设置——》个人资料——》基本资料——》出生地(注册地)为例来分析:


概述:一、如图出生地,注册地数据是通过get参数传递读取数据库的资料,然后通过模板显示出来的:大体数据流是 /home.php ——》 /source/module/home/home_spacecp.php ——》 /source/include/spacecp/spacecp_profile.php 将用户个人资料放在变量$htmls中,在模板/template/default/home/spacecp_profile.htm中显示出来的;
二、点击修改就会弹出省市县的列表框,是通过ajax弹出,并选择上一级后,通过ajax自动更新下一级列表框,用到的js方法是showdistrict()通过程序文件/source/include/misc/misc_ajax.php和模板文件/template/default/home/misc_ajax.htm来实现的。

详述:一、(1)有域名home.php?mod=spacecp&ac=profile&op=base知道入口文件是/home.php ,有mod=spacecp知道引用的,模块文件是/source/module/home/home_spacecp.php ,模块文件又引入程序文件/source/include/spacecp/spacecp_profile.php ,
(2)先看模板文件/template/default/home/spacecp_profile.htm的126—146行代码:
  1. <!--{loop $settings $key $value}-->
  2. <!--{if $value[available]}-->
  3. <tr id="tr_$key">
  4. <th id="th_$key"><!--{if $value[required]}--><span class="rq" title="{lang required}">*</span><!--{/if}-->$value[title]</th>
  5. <td id="td_$key">
  6. $htmls[$key]
  7. </td>
  8. <td class="p">
  9. <!--{if $value[showinthread] || $vid}-->
  10. <input type="hidden" name="privacy[$key]" value="3" />
  11. <!--{else}-->
  12. <select name="privacy[$key]">
  13. <option value="0"{if $privacy[$key] == "0"} selected="selected"{/if}>{lang open_privacy}</option>
  14. <option value="1"{if $privacy[$key] == "1"} selected="selected"{/if}>{lang friend_privacy}</option>
  15. <option value="3"{if $privacy[$key] == "3"} selected="selected"{/if}>{lang secrecy}</option>
  16. </select>
  17. <!--{/if}-->
  18. </td>
  19. </tr>
  20. <!--{/if}-->
  21. <!--{/loop}-->
复制代码
$value[title]是每一项资料的名称,$htmls[$key]是每一项资料的已填写结果;
(3)我们再回来看程序文件/source/include/spacecp/spacecp_profile.php ,$value[title]是这样取得的看408--416行代码:
  1. $htmls = $settings = array();
  2. foreach($allowitems as $fieldid) {
  3. if(!in_array($fieldid, array('sightml', 'customstatus', 'timeoffset'))) {
  4. $html = profile_setting($fieldid, $space, $vid ? false : true);
  5. if($html) {
  6. $settings[$fieldid] = $_G['cache']['profilesetting'][$fieldid];
  7. $htmls[$fieldid] = $html;
  8. }
  9. }
  10. }
复制代码
用了函数profile_setting,这个函数是哪来的呢?上面文件第36行就引入了函数文件
  1. include_once libfile('function/profile');
复制代码
原函数内容是这样的:在/source/function/function_profile.php文件328—378行,详细可以看代码了解一下,并且在函数中当是地区信息时 输出了ajax函数。

二、(1)ajax的实现,看/source/function/function_profile.php的profile_setting函数当是地区信息时,它输出的一句代码是这样的:

onclick="showdistrict('birthdistrictbox', ['birthprovince', 'birthcity', 'birthdist', 'birthcommunity'], 4); return false;" 这句js方法是调用的static/js/common.js这个文件的js函数,看到1814行到1816行;
  1. function showdistrict(container, elems, totallevel, changelevel) {
  2. $F('_showdistrict', arguments);
  3. }
复制代码
(2)由$F('_showdistrict', arguments);又到了JS文件/static/js/common_extra.js ,_showdistrict这个函数在139-152行,
  1. function _showdistrict(container, elems, totallevel, changelevel) {
  2. var getdid = function(elem) {
  3. var op = elem.options[elem.selectedIndex];
  4. return op['did'] || op.getAttribute('did') || '0';
  5. };
  6. var pid = changelevel >= 1 && elems[0] && $(elems[0]) ? getdid($(elems[0])) : 0;
  7. var cid = changelevel >= 2 && elems[1] && $(elems[1]) ? getdid($(elems[1])) : 0;
  8. var did = changelevel >= 3 && elems[2] && $(elems[2]) ? getdid($(elems[2])) : 0;
  9. var coid = changelevel >= 4 && elems[3] && $(elems[3]) ? getdid($(elems[3])) : 0;
  10. var url = "home.php?mod=misc&ac=ajax&op=district&container="+container
  11. +"&province="+elems[0]+"&city="+elems[1]+"&district="+elems[2]+"&community="+elems[3]
  12. +"&pid="+pid + "&cid="+cid+"&did="+did+"&coid="+coid+'&level='+totallevel+'&handlekey='+container+'&inajax=1'+(isUndefined(changelevel) ? '&showdefault=1' : '');
  13. ajaxget(url, container, '');
  14. }
复制代码
ajax到这里才体现出来,ajax的程序文件的url是home.php?mod=misc&ac=ajax&op=district这个;
(3)有入口文件home.php和mod=misc知道模块文件是/source/include/misc/misc_ajax.php,这个文件在250-251行代码:
  1. include_once libfile('function/profile');
  2. $html = showdistrict($values, $elems, $container, $showlevel);
复制代码
通过引用函数/source/function/function_profile.php的showdistrict()调用选择的省或市或县或镇级的地区数据;showdistrict()这个函数输出的内容中又有onchange="showdistrict('birthdistrictbox', ['birthprovince', 'birthcity', 'birthdist', 'birthcommunity'], X, X)"这个JS函数 ,然后ajax调用下一级的地区的数据;
(4)并输出在模板template/default/home/misc_ajax.htm中,模板中没有任何html代码,就函数的输出内容:模板的48-50行代码是这样写的:
  1. <!--{elseif $op == 'district'}-->
  2. $html
  3. <!--{/if}-->
复制代码

最后ajax调用的数据显示在/template/default/home/spacecp_profile.htm这个模板页中。


补充:为了实际应用,补充篇帖子《Discuz! X2 地区数据 调用实例》;URL地址: https://discuz.dismall.com/thread-2370241-1-1.html



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

评分

1

查看全部评分

股商论坛 发表于 2011-9-12 19:14:25 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

 楼主| otherbank 发表于 2011-9-12 19:15:29 | 显示全部楼层
股商论坛 发表于 2011-9-12 19:14
多谢分享!!!!!!!!

回复

使用道具 举报

ARCHY` 发表于 2011-9-12 20:07:30 | 显示全部楼层
没抢到沙发  支持下  学习中
回复

使用道具 举报

eqmz 发表于 2011-9-13 16:51:43 | 显示全部楼层
多谢分享!!!!!!!!
回复

使用道具 举报

导弹狼 发表于 2011-9-14 11:51:42 | 显示全部楼层
这个帖子要顶,赞啊!
回复

使用道具 举报

 楼主| otherbank 发表于 2011-9-14 12:00:23 | 显示全部楼层
导弹狼 发表于 2011-9-14 11:51
这个帖子要顶,赞啊!

真有缘,因为我知道你也是ccidcom出来的
回复

使用道具 举报

导弹狼 发表于 2011-9-14 13:53:26 | 显示全部楼层
otherbank 发表于 2011-9-14 12:00
真有缘,因为我知道你也是ccidcom出来的

,是的,往事不堪回首啊
回复

使用道具 举报

soqos 发表于 2011-9-14 17:13:02 | 显示全部楼层
真想不明白,简单的一段程序,搞成这么复杂。哎。
回复

使用道具 举报

shwxyuan 发表于 2011-9-18 18:29:04 | 显示全部楼层
www.hnxwz.com好东西
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2024-6-26 09:41 , Processed in 0.143803 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表