[ASP] 在下拉列表中输出树型结构(原作)

(我以前发表的一篇旧文章,现在统一整理到这里来。)

SQL Server 2000 中用于建表以及其他脚本的 SQL 如下:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /* -------------------------------------
InBlock.gif功能: 建立表 tree 以及两个自定义函数
InBlock.gif        用于层次性的输出到 <select>
InBlock.gifAuthor: Neil Chen(木野狐)
InBlock.gifDate: 2004-3-19
ExpandedBlockEnd.gif---------------------------------------
*/

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 创建表 */
None.gif
use  test
None.gif
go
None.gif
None.gif
DROP   TABLE  tree
None.gif
DROP   FUNCTION   [ dbo ] .getDepth
None.gif
DROP   FUNCTION   [ dbo ] .getCode
None.gif
go
None.gif
None.gif
create   table  tree
None.gif(
None.gif    id 
int   identity   primary   key ,
None.gif    pid 
int ,
None.gif    title 
varchar ( 50 )
None.gif)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 初始化数据 */
None.gif
insert  tree  select   0 ' 中国 '
None.gif
insert  tree  select   1 ' 江苏 '
None.gif
insert  tree  select   1 ' 山东 '
None.gif
insert  tree  select   2 ' 南京 '
None.gif
insert  tree  select   2 ' 无锡 '
None.gif
insert  tree  select   1 ' 四川 '
None.gif
None.gif
go
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* ------------------------------
InBlock.gif函数: getDepth
InBlock.gif功能: 得到深度, 根的深度为 0 
ExpandedBlockEnd.gif------------------------------ 
*/

None.gif
CREATE   FUNCTION   [ dbo ] .getDepth( @id   INT )
None.gif
returns   INT  
None.gif
AS  
None.gif
BEGIN  
None.gif
None.gif
DECLARE   @depth   int @temp   int
None.gif
SET   @depth   =   0
None.gif
None.gif
WHILE  ( SELECT  pid  FROM  tree  WHERE  id  =   @id >   0
None.gif
BEGIN  
None.gif    
SELECT   @id   =  pid  FROM  tree  WHERE  id  =   @id
None.gif    
SET   @depth   =   @depth   +   1
None.gif
END  
None.gif
None.gif
RETURN   @depth
None.gif
None.gif
END  
None.gif
None.gif
go  
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* ------------------------------
InBlock.gif函数: getCode
InBlock.gif功能: 得到编码
ExpandedBlockEnd.gif------------------------------ 
*/

None.gif
CREATE   FUNCTION   [ dbo ] .getCode( @id   int )
None.gif
RETURNs   VARCHAR ( 10 )
None.gif
AS  
None.gif
None.gif
BEGIN  
None.gif
None.gif
DECLARE   @code   VARCHAR ( 10
None.gif
SET   @code   =   ''
None.gif
None.gif
WHILE   @id   >   0
None.gif
BEGIN  
None.gif    
SELECT   @code   =   CAST ( COUNT (id)  AS   VARCHAR ( 10 ))  +   @code
None.gif    
FROM  tree
None.gif    
WHERE   [ dbo ] .getDepth(id)  =   [ dbo ] .getDepth( @id )
None.gif        
AND  id  <=   @id
None.gif
None.gif    
SELECT   @id   =  pid  FROM  tree  WHERE  id  =   @id
None.gif
END  
None.gif
None.gif
RETURN   @code
None.gif
None.gif
END  
None.gif    
None.gif
go  
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 测试 */
None.gif
SELECT   * [ dbo ] .getCode(id)  AS  code
None.gif
FROM  tree
None.gif
ORDER   BY  code
None.gif
None.gif
go

对应的用于输出的 ASP 程序:

None.gif < !DOCTYPE HTML  PUBLIC   " -//W3C//DTD HTML 4.0 Transitional//EN " >
None.gif
< HTML >
None.gif
< HEAD >
None.gif
< TITLE >   New  Document  </ TITLE >
None.gif
< META NAME = " Generator "  CONTENT = " EditPlus " >
None.gif
< META NAME = " Author "  CONTENT = " Neil Chen(木野狐) " >
None.gif
< meta http - equiv = " Content-Type "  content = " text/html; charset=gb2312 " >
None.gif
</ HEAD >
None.gif
None.gif
< BODY >
None.gif
None.gif
< %
None.gif
dim  conn
None.gif
set  conn  =  server. CreateObject ( " ADODB.Connection " )
None.gifconn.open 
" Provider=SQLOLEDB.1;Password=markchen;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=cr"
None.gif

None.gif
dim  sql, rs
None.gifsql 
=   " SELECT *, [dbo].getCode(id) AS code from tree order by code"
None.gif
set  rs  =  conn. execute (sql)
None.gif
None.gifresponse.write 
" <select>"
None.gif

None.gif
do   while   not  rs.eof
None.gif  response.write 
" <option>"
None.gif
   ' 输出缩进
None.gif
   for  i  =   2   to   len (rs( " code " ))
None.gif    response.write 
" &nbsp;&nbsp;"
None.gif
   next
None.gif  response.write rs(
" title " &   " &nbsp;&nbsp; "   &  rs( " code " &   " </option>"
None.gif
  rs.MoveNext
None.gif
loop
None.gif
None.gifresponse.write 
" </select>"
None.gif

None.gif
rs.close :  set  rs  =   nothing
None.gifconn.close : 
set  conn  =   nothing
None.gif%
>
None.gif
None.gif
</ BODY >
None.gif
</ HTML >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值