本帖最后由 zhaoxun2005 于 2010-11-6 18:46 编辑
先声明:此方法是看了bqqcat的帖子,然后修改而成!
亲子在线网 www.qinzi001.com 首页调用的儿歌就是用这个做的!可以参看下!
增加SQL调用,只需创建一个文件,在“source\class\block\html” 文件夹下创建名为:“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);
}
}
?> |