科汛很多数据以内容集的形式存放于是一个字段,比如图片系统里的图片地址。各个图片地址以|||隔开存放于PicUrls这个字段里,让建站者头疼的是怎样调用单个图片地址?由于笔者现需实现随机调取单个数据,因此从SQL标签下手开发出灵活运用的拆分数据标签。
本方法未经验证,如果仅需要输出图片系统的图片地址,可参考 http://miaojingyun.com/post/463.html
标签:{$SPLITDATA(aaa[]b[]c)}
说明:aaa:字段;b:分隔符;c:输出数据所在位置(从0开始),当c=-1时随机输出拆分项
案例:{$SPLITDATA({$Field(PicUrls,Text,0,...,0,)}[],[]-1)}
创建该标签需修改同一asp三处:
/KS_Cls/Kesion.Label.SQLCls.asp
第一处:
Set objNode=Application(KS.SiteSN&"_sqllabellist").documentElement
For i=0 to objNode.ChildNodes.length-1
set objAtr=objNode.ChildNodes.item(i)
Str=Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(objAtr.Attributes.item(2).Text&"","{$Field","{#Field"),"{$AutoID}","{#AutoID}"),"{$DaoXiID}","{#DaoXiID}"),"{$IF","{#IF"),"{$Param","{#Param"),"{$GetItemUrl}","{#GetItemUrl}"),"{$REPLACE","{#REPLACE"),"{$SPLITDATA","{#SPLITDATA")
'避免Field字段被替换掉,先转为#
Str=Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Str,"{$CurrClassID}","{#CurrClassID}"),"{$CurrTopClassID}","{#CurrTopClassID}"),"{$CurrClassName}","{#CurrClassName}"),"{$CurrChannelID}","{#CurrChannelID}"),"{$CurrClassChildID}","{#CurrClassChildID}"),"{$CurrUserName}","{#CurrUserName}"),"{$CurrInfoID}","{#CurrInfoID}"),"{$CurrSpecialID}","{#CurrSpecialID}"),"{$GetUserName}","{#GetUserName}")
Str=Rcls.ReplaceGeneralLabelContent(Str)
Str=Replace(Replace(Replace(Replace(Replace(Replace(Replace(Str,"{#Field","{$Field"),"{#AutoID}","{$AutoID}"),"{#DaoXiID}","{$DaoXiID}"),"{#IF","{$IF"),"{#Param","{$Param"),"{#REPLACE","{$REPLACE"),"{#SPLITDATA","{$SPLITDATA")
objAtr.Attributes.item(2).Text=Str
Next
set Rcls=nothing
第二处:
ReplaceCondition1=TempStr
End Function
'拆分数据输出其中一项 {$SPLITDATA(aaa[]b[]c)} aaa:拆分数据 b:分隔符 c:提取哪项范围,当c=-1时随机输出拆分项
Function ReplaceCondition3(byval str)
Dim regEx, Matches, Match, TempStr,Bool
Dim FieldParam,FieldParamArr,ReturnFieldValue,I,FieldParamArr1,k
on error resume next
Set regEx = New RegExp
regEx.Pattern = "{\$SPLITDATA\([^{\$}]*}"
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(str)
TempStr=str
For Each Match In Matches
FieldParam = Replace(Replace(Match.Value,"{$SPLITDATA(",""),")}","")
FieldParamArr = Split(FieldParam,"[]")
if Ubound(FieldParamArr)=2 then
I=int(FieldParamArr(2))
if FieldParamArr(0)<>"" and FieldParamArr(1)<>"" then
FieldParamArr1=Split(FieldParamArr(0),FieldParamArr(1))
k=Ubound(FieldParamArr1)
if I>k then
ReturnFieldValue=FieldParamArr(0) '超出拆分范围输出自身
else
if I<0 then
Randomize Timer
I=Int((k+1)*Rnd)
ReturnFieldValue=FieldParamArr1(I) '输出随机项
else
ReturnFieldValue=FieldParamArr1(I) '输出指定项
end if
end if
end if
end if
if err then
err.clear
else
TempStr=Replace(TempStr,"{$SPLITDATA(" &FieldParam &")}",ReturnFieldValue)
end if
Next
ReplaceCondition3=TempStr
End Function
'取得分页标志
Function GetPageStr(SqlLabel)
第三处:
CleanLabel=ReplaceCondition(Replace(Content,"$KS:Page$",vbcrlf))
CleanLabel=ReplaceCondition1(CleanLabel)
CleanLabel=ReplaceCondition3(CleanLabel)
End Function
'替换循环部分内容
Function GetCirLabelContent(CirLabelContent,ByRef KS_RS_Obj,LoopTimes,ByVal NN)
备注:红色为添加部分
举例:
sql语句:
select top 1 FolderName,ClassDefineContent from KS_class
内容:
[loop=1]{$Field(foldername,Text,0,...,0,)}{$SPLITDATA({$Field(ClassDefineContent,Text,0,...,2,)}[],[]1)}[/loop]
输出以英文逗号隔开第二项
相关文章