IT618

 找回密码
 立即注册
查看: 1635|回复: 0

Discuz!利用ajax从数据库读取论坛版块主题数

[复制链接]
<

267

主题

49

回帖

3087万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
30870559
发表于 2016-3-20 09:14:45 | 显示全部楼层 |阅读模式
有时需要显示某个指定的论坛版块的主题数,这个肯定是要写代码的,然而Discuz是不方便写代码到某个模板里的,虽然可以实现,但是我们可以ajax调用。

实现这个功能要写JS代码与PHP代码:
js代码如下:
function ajax_driv() {
    var xmlhttp;
    if (window.ActiveXObject) {
        /* 不要删除以下注释,这部分不是注释 */
        /*@cc_on @*/
        /*@if (@_jscript_version >= 5)
  try {
    xmlhttp = new ActiveXObject("Msxml2.xmlhttp");
  } catch (e) {
    try {
   xmlhttp = new ActiveXObject("Microsoft.xmlhttp");
    } catch (e) {
   xmlhttp = false;
    }
  }
  @end @*/
    } else {
        xmlhttp = new XMLHttpRequest();
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

function getposts(fid) {
    var xmlhttp = false;

xmlhttp = ajax_driv();

xmlhttp.open("GET", "getforumpost.php?fid=" + fid, true);

xmlhttp.setRequestHeader("If-Modified-Since", "Thu, 01 Jan 1970 00:00:00 GMT");
xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4) {
   document.getElementById("fl" + fid).innerHTML="(" + xmlhttp.responseText + ")";
  }
}
xmlhttp.send(null);
}

getforumpost.php文件代码如下:
<?php
连接数据库部分代码省略
$fid = (int) $_GET['fid'];
$exec="select concat(concat(todayposts,'/'),posts) as myposts from dz_forum_forum where fid=" . $fid;
$result=mysql_query($exec);
if($rs=mysql_fetch_object($result)){echo $rs->myposts;}
else{echo "0";}
?>

模板调用代码:
版块名称<label id="fl54">(<script>getposts(54);</script>)</label>

php代码里的读取数据库可以直接引用DZ的数据库类
require './source/class/class_core.php';//引入系统核心文件
echo DB::result_first("SELECT concat(concat(todayposts,'/'),posts) FROM ".DB::table('forum_forum')." where fid=".$fid);


回复

使用道具 举报

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

本版积分规则

手机版|插件教程|常见问题|扫码访问手机版|IT618

GMT+8, 2024-5-17 11:21

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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