城市里的游牧民族

Menu

DISCUZ X2最新回复调用方法(无插件-用SQL)

今天想给兰州流浪客业余足球论坛实现无插件调用最新回复,在网上查了相关资料,本来可以使用插件,但是因为模板的原因,所以只能是用其他方法。

首页的最新回复,案例查看:www.liulangke.org

--------------------------------------------------------

注意:如果在后台添加新的静态模块数据调用,在静态模块的数据来源里面如果没有SQL调用,那么就按照如下步骤来添加SQL调用(门户—模块管理—数据调用—模块分类—静态模块—添加调用—数据源—SQL调用)

第一步:增加SQL调用数据源

1、新建block_sql.php

2、将如下代码复制粘贴到block_sql.php里面

<?php
/**
*     

*    
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
require_once libfile('commonblock_html', 'class/block/html');
class block_sql extends commonblock_html {
function block_sql() {}
function name() {
  return 'SQL脚本';
}
function getsetting() {
  global $_G;
  $settings = array(
   'sql' => array(
    'title' => 'SQL语句',
    'type' => 'textarea',
    'default' => 'SELECT *
FROM `pre_forum_forum`'
   ),
  
   /*'fieldlist' => array(
    'title' => '可引用字段',
    'type' => 'textarea',
    'default' => '{$tablepre} '
   ),*/
     
   'template' => array(
    'title' => 'HTML模板',
    'type' => 'textarea',
    'default' => '[node]{name}<BR>[/node]'
   ),
  
   'start' => array(
    'title' => '起始数据行数',
    'type' => 'text',
    'default' => 0
   ),
   'limit' => array(
    'title' => '显示数据条数',
    'type' => 'text',
    'default' => 5
   )
  );
  return $settings;
}
function getdata($style, $parameter) {
  require_once libfile('function/home');
///  $return = getstr($parameter['content'], '', 1, 0, 1, 0, 1);
///
global $_G;
$tablepre = $_G['config']['db']['1']['tablepre'];
$sql = !empty($parameter['sql']) ? ($parameter['sql']) : '';
$start = !empty($parameter['start']) ? intval($parameter['start']) : 0;
$limit = !empty($parameter['limit']) ? intval($parameter['limit']) : 5;

$writedata = '';
if ($sql != '')
{
  $searchs1 = $replaces1 = array();
  $searchs1[] = '{$tablepre}';
  $replaces1[] = $tablepre;//'`'.$dbname.'`.'.$tablepre;
  $sql = str_replace($searchs1, $replaces1, stripslashes($sql));
  $sql = ltrim(strtolower($sql));
  $i = strpos($sql , 'select');
  if ($i != 0){
   $writedata = '只能定义SELECT语句';
   return array('html' => $writedata, 'data' => null);
  }
  $sqldata = $sql.' limit '.$start.','.$limit.';';
  $query = DB::query($sqldata);

  $writedata = '';
  $requesttemplatebody = '';
  $requesttemplate = stripslashes($parameter['template']);
  if(preg_match("/\[node\](.+?)\[\/node\]/is", $requesttemplate, $node)) {
   $requesttemplatebody = $requesttemplate;
   $requesttemplate = $node[1];
  }
  while($thread = DB::fetch($query)) {
   $searchs = $replaces = array();
   foreach(array_keys($thread) as $key) {
    $searchs[] = '{'.$key.'}';
    $replaces[] = htmlspecialchars($thread[$key]);
    $searchs[] = '{rawurlencode('.$key.')}';
    $replaces[] = rawurlencode($thread[$key]);
   }
   $writedata .= str_replace($searchs, $replaces, $requesttemplate);
  }
  if ($requesttemplatebody){
   $oldwritedata = $writedata;
   $writedata = str_replace($node[0], $oldwritedata, $requesttemplatebody);
  }
}else{
$writedata = '没有定义SQL';
}
///
  return array('html' => $writedata, 'data' => null);
}
}
?>

3、将刚刚新建的block_sql.php上传保存到 source\class\block\html目录下

4、然后在 source/language/ 找到 lang_blockclass.php文件 在 'blockclass_html_script_category' => '分类信息', 下面加上'blockclass_html_script_sql' => 'SQL脚本', 保存。

第二步:用SQL的方法实现最新回复

1、添加一个静态模块的数据调用

后台—门户—模块管理—数据调用—模块分类—静态模块—添加调用

2、模块属性里面的数据来源便会看到有SQL调用(刚刚第一步就是增加的这个)
输入模块标识:最新回复
数据来源选择SQL调用
SQL语句里面输入

SELECT * FROM pre_forum_thread s,pre_forum_forum st where s.replies>1 and s.tid>1 and s.fid=st.fid and st.name<>1 group by s.subject order by s.lastpost desc

HTML模板里面输入

<div class="module cl xl xl1">
<ul>
[node]
<li><span>[<a href="forum-{fid}-1.html"  target="_blank">{name}</a>]</span><a href="thread-{tid}-1-1.html" title="{name}" target="_blank">{subject}</a></li>
[/node]
</ul>
</div>

这个html模板,具体根据实际情况自己定义

其他的就可以默认了。
最后确认

第三步:将最新回复的内部调用代码粘贴到首页模板你需要的位置。

1、后台—门户—模块管理—数据调用—模块列表—最新回复—内部调用,点击得到内部调用代码
2、找到你的首页文件模板,在恰当位置ctrl+v。

所有工作全部完成
如有不清楚欢迎大家留意。

--------------------------------------------------------

感谢网友提供方法:http://www.discuz.net/thread-2230248-1-1.html

                                      http://www.discuz.net/thread-2356809-1-1.html

— 于 共写了3630个字
— 文内使用到的标签:
本作品采用知识共享署名-非商业性使用-禁止演绎 3.0 中国大陆许可协议进行许可。

2条回应:“DISCUZ X2最新回复调用方法(无插件-用SQL)”

  1. 铝合金压铸说道:

    绝对支持版主,博主每天辛苦更新文章不容易,我会经常来访问你的站点的,文章写的好。收藏一下。顶

2哥进行回复 取消回复

电子邮件地址不会被公开。 必填项已用*标注