<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="../inc/conn.asp"-->
<!--#include file="../inc/function.asp"-->
<%
On error resume next
key=trim(request("key"))
sid=trim(request("sid"))
page=trim(request("page"))'当前页数

if key="" or sid="" then Response.Write("<script>alert('非法搜索');history.back();</script>")
if sid=1 then sq="select * from cw_news where title like '%"&key&"%' order by id desc"
if sid=2 then sq="select * from cw_info where title like '%"&key&"%' order by id desc"
if sid=3 then sq="select * from cw_shop where title like '%"&key&"%' or service like '%"&key&"%' order by id desc"
if sid=4 then sq="select * from cw_hr where zhuanye like '%"&key&"%' or xingming like '%"&key&"%' order by id desc"
if sid=5 then sq="select * from cw_job where zhiwei like '%"&key&"%' or title like '%"&key&"%' order by id desc"
if sid=6 then sq="select * from cw_bbs where title like '%"&key&"%' or content like '%"&key&"%' order by id desc"
if sid=7 then sq="select * from cw_hous where title like '%"&key&"%' or type like '%"&key&"%' order by id desc"
select case sid
  case 1
     tab="cw_news"
  case 2
     tab="cw_info"
  case 3
     tab="cw_shop"
  case 4
     tab="cw_hr"
  case 5
     tab="cw_job"
  case 6
     tab="cw_bbs"
  case 7
     tab="cw_hous"
end select
pags=10 '每页记录数
if key<>"" and sid<>"" then
data=cw_redtemp(cf_root&"skins/"&cf_skin,tp_search)
data=cw_include(data)     
data=cw_system(data)
data=Replace(data,"{cw:keyword}",key)
data=Replace(data,"{cw:keysid}",sid)
data=cw_loop(data)
dpage=cw_searchpag(sq,pags)'获取分页数
if dpage=0 then page=1
if page="" then page=1
if cint(page)>cint(dpage) then page=dpage
  html=data
  Set Reg = New RegExp
  Reg.IgnoreCase = True
  Reg.Global = True
  Reg.Pattern="{@cw:search}([\s\S]+?){/@cw:search}"
  Set Matches=Reg.Execute(html)
  IF Matches.Count>0 Then
     For Each Match In Matches
      f2=Match.SubMatches(0)'循环内容
	  '//匹配表来决定展现形式
	  set Reg2=New RegExp
	  Reg2.IgnoreCase=True
	  Reg2.Global=True
	  Reg2.Pattern="\[cw_([\s\S]+?)\]([\s\S]+?)\[/cw_([\s\S])+?\]"
	  Set Math=Reg2.Execute(f2)
	  If Math.Count>0 then
	     For Each Mat In Math
		   m1=Mat.SubMatches(0)
		   If tab="cw_"&m1 then
		     m2=Mat.SubMatches(1)
		   End If	   
		 Next
	  End If
	  '//End	   
      f4=Cw_page_sql(sq,m2,pags,page)
      html=Replace(html,Match.value,f4)'for循环内容替换	  
      html=Replace(html,"{cw:listpage}",cw_searchpage(sq,pags,page,key,sid))'替换分页标签
     Next
  End If
  data=html
  Set Reg=nothing
  set Matches=nothing
data=cw_function(data)
data=cw_if(data)
response.Write(data)
end if

Function cw_searchpage(sql,pag,pa,key,sid)
 key=server.URLEncode(key)
 set rl=server.CreateObject("adodb.recordset") 
 rl.open sql,conn,1,1
 if not rl.eof then
  page=pa
  n=rl.RecordCount
  rl.pagesize=pag
  m=cint(rl.pagecount)
  if page="" then 
   page=1
  elseif page<1 then 
    page=1
  end if
  if cint(pa)>m then
        page=m
     end if
  page=cint(page)
  if not rl.eof then 
     rl.absolutepage=page
  end if     
  if m>page+3 then 
     endpage=page+3 
  else 
     endpage=m 
  end if 
  if pa=1 then
     pagetitle=pagetitle&"<a>首页</a>"
  else
     pagetitle=pagetitle&"<a href='?key="&key&"&sid="&sid&"'>首页</a>"
  end if
  if pa>1 then
     if pa=2 then
     pagetitle=pagetitle&"<a href='?key="&key&"&sid="&sid&"'>上一页</a>"
     else
     pagetitle=pagetitle&"<a href='?key="&key&"&page="&pa-1&"&sid="&sid&"'>上一页</a>"
     end if
  else
     pagetitle=pagetitle&"<a>上一页</a>"
  end if
  if page>4 then
     pagetitle=pagetitle&"<a href='?key="&key&"&page=4&sid="&sid&"'>...</a>"
  end if     
  for i=page-3 to endpage
     if not i<1 then 
        if i = page then 
          pagetitle=pagetitle&"<span>"&i&"</span>"     
        else 
          if i=1 then
             pagetitle=pagetitle&"<a href='?key="&key&"&sid="&sid&"'>"&i&"</a>"
          else
             pagetitle=pagetitle&"<a href='?key="&key&"&page="&i&"&sid="&sid&"'>"&i&"</a>"
          end if
        end if 
     end if 
  next 
  if page+3 < m then
     pagetitle=pagetitle&"<a href='?key="&key&"&page="&page+3&"&sid="&sid&"'>...</a>"
  end if 
  if rl.pagecount<pa+1 then
     pagetitle=pagetitle&"<a>下一页</a>"
  else
     pagetitle=pagetitle&"<a href='?key="&key&"&page="&pa+1&"&sid="&sid&"'>下一页</a>"
  end if
  if rl.pagecount=<pa+1 then
     pagetitle=pagetitle&"<a>尾页</a>"
  else
     pagetitle=pagetitle&"<a href='?key="&key&"&page="&rl.pagecount&"&sid="&sid&"'>尾页</a>"
  end if
  pagetitle=pagetitle&"<a>"&page&"/"&rl.pagecount&"页</a>"
 end if 
 cw_searchpage=pagetitle
 rl.close
 set rl=nothing
End Function

'//获是分页数量
Function cw_searchpag(sql,pag)
      set rp=server.CreateObject("adodb.recordset")
      rp.open sql,conn,1,1
      rp.pagesize=pag
      cw_searchpag=rp.pagecount
      rp.close
      set rp=nothing
End Function
%>
