loop.lib.php
2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
if(!defined('DEDEINC'))
{
exit("Request Error!");
}
/**
* 调用任意表的数据标签
*
* @version $Id: loop.lib.php 1 9:29 2010年7月6日Z tianya $
* @package DedeCMS.Taglib
* @copyright Copyright (c) 2007 - 2010, DesDev, Inc.
* @license http://help.dedecms.com/usersguide/license.html
* @link http://www.dedecms.com
*/
/*>>dede>>
<name>万能循环</name>
<type>全局标记</type>
<for>V55,V56,V57</for>
<description>调用任意表的数据标签</description>
<demo>
{dede:loop table='dede_archives' sort='' row='4' if=''}
<a href='[field:arcurl/]'>[field:title/]</a>
{/dede:loop}
</demo>
<attributes>
<iterm>table:查询表名</iterm>
<iterm>sort:用于排序的字段</iterm>
<iterm>row:返回结果的条数</iterm>
<iterm>if:查询的条件</iterm>
</attributes>
>>dede>>*/
require_once(DEDEINC.'/dedevote.class.php');
function lib_loop(&$ctag,&$refObj)
{
global $dsql;
$attlist="table|,tablename|,row|8,sort|,if|,ifcase|,orderway|desc";//(2011.7.22 增加loop标签orderway属性 by:织梦的鱼)
FillAttsDefault($ctag->CAttribute->Items,$attlist);
extract($ctag->CAttribute->Items, EXTR_SKIP);
$innertext = trim($ctag->GetInnertext());
$revalue = '';
if(!empty($table)) $tablename = $table;
if($tablename==''||$innertext=='') return '';
if($if!='') $ifcase = $if;
if($sort!='') $sort = " ORDER BY $sort $orderway ";
if($ifcase!='') $ifcase=" WHERE $ifcase ";
$dsql->SetQuery("SELECT * FROM $tablename $ifcase $sort LIMIT 0,$row");
$dsql->Execute();
$ctp = new DedeTagParse();
$ctp->SetNameSpace("field","[","]");
$ctp->LoadSource($innertext);
$GLOBALS['autoindex'] = 0;
while($row = $dsql->GetArray())
{
$GLOBALS['autoindex']++;
foreach($ctp->CTags as $tagid=>$ctag)
{
if($ctag->GetName()=='array')
{
$ctp->Assign($tagid, $row);
}
else
{
if( !empty($row[$ctag->GetName()])) $ctp->Assign($tagid,$row[$ctag->GetName()]);
}
}
$revalue .= $ctp->GetResult();
}
return $revalue;
}