xml+php动态载入与分页

这是在看太平洋网的评论时看到的,太平洋网是用jsp做为后台语言,用来产生xml文件.然后在把数据绑定到html上的.我就用php也做了一个以下是源文件.

-----------------xml.htm------------------

 1<html>
 2<head>
 3<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 4<title>xml</title>
 5</head>
 6<body>
 7<xml id="ShopList" src="xml.php"></xml>
 8<script>   
 9function jump(form)   
10{   
11if (form.pageNum.value>parseInt(pageCount.innerHTML)) return false;   
12if (form.pageSize.value>parseInt(recordCount.innerHTML)) return false;   
13ShopList.src="xml.php?pageNo="+form.pageNum.value+"&pageSize="+form.pageSize.value+"&time="+(new Date()).getTime();   
14}   
15function pageCount_onpropertychange(form)   
16{   
17if (form.pageNum.value>parseInt(pageCount.innerHTML)) form.pageNum.value=parseInt(pageCount.innerHTML);   
18if (form.pageSize.value>parseInt(recordCount.innerHTML)) form.pageSize.value=parseInt(recordCount.innerHTML);   
19if(form.pageNum.value==1)   
20{   
21form.firstPage.disabled=true;   
22form.previousPage.disabled=true;   
23}   
24else   
25{   
26form.firstPage.disabled=false;   
27form.previousPage.disabled=false;   
28}   
29if(form.pageNum.value==form.pageCount.value)   
30{   
31form.nextPage.disabled=true;   
32form.lastPage.disabled=true;   
33}   
34else   
35{   
36form.nextPage.disabled=false;   
37form.lastPage.disabled=false;   
38}   
39}   
40</script>
41<table border="0" cellpadding="2" cellspacing="1" datafld="商铺" datasrc="#ShopList" height="25" id="shopTbl" width="100%">
42<thead>
43<tr>
44<td colspan="4" height="25"><span class="shop_3d_text">店铺列表</span></td>
45</tr>
46<tr>
47<td width="30%">商铺名称</td>
48<td width="20%">经营者</td>
49<td width="20%">店铺级别</td>
50<td width="30%">店铺简介</td>
51</tr>
52</thead>
53<tbody>
54<tr style="background-color:expression(rowIndex%2==1?'#EFEFEF':'#FFFFFF')">
55<td><a datafld="连接地址"><span datafld="名称">名称</span></a></td>
56<td><span datafld="经营人">经营人</span></td>
57<td><span datafld="级别">级别</span></td>
58<td><span datafld="简介">简介</span></td>
59</tr>
60</tbody>
61</table>
62<form onsubmit="jump(this);return false" return="">
63<div align="right">   
64每页显示   
65<input datafld="页大小" datasrc="#ShopList" name="pageSize" size="2"/>条,   
66<input style="width:0" type="submit" value=""/>   
67共<span datafld="页数量" datasrc="#ShopList" id="pageCount"></span>页   
68<span datafld="数量" datasrc="#ShopList" id="recordCount"></span>条   
69  
70<input name="firstPage" onclick="pageNum.value=1" type="submit" value="首页"/>
71<input name="previousPage" onclick="pageNum.value=pageNum.value-1" type="submit" value="上页"/>   
72第<input datafld="页号码" datasrc="#ShopList" name="pageNum" size="2"/>页   
73<input name="nextPage" onclick="pageNum.value=(pageNum.value-0)+1" type="submit" value="下页"/>
74<input name="lastPage" onclick="pageNum.value=pageCount.value" type="submit" value="尾页"/>
75<input datafld="页数量" datasrc="#ShopList" name="pageCount" onpropertychange="pageCount_onpropertychange(form)" type="hidden"/>
76</div> </form>
77</body>
78</html>

-----------------xml.php------------------

  1   
  2//require_once "Source/Global.php";   
  3function getVars($get_vars, $post_vars) { 
  4
  5$i = count($get_vars);   
  6$j = count($post_vars);   
  7if ($i > 0 && $j > 0 ) {   
  8while(list($key, $val) = each($get_vars)) {   
  9$Vars[$key] = $val;   
 10}   
 11while(list($key2, $val2) = each($post_vars)) {   
 12$Vars[$key2] = $val2;   
 13}   
 14} elseif ($i > 0) {   
 15while(list($key, $val) = each($get_vars)) {   
 16$Vars[$key] = $val;   
 17}   
 18} elseif ($j > 0) {   
 19while(list($key2, $val2) = each($post_vars)) {   
 20$Vars[$key2] = $val2;   
 21}   
 22} else {   
 23$Vars = array();   
 24}   
 25return $Vars;   
 26} 
 27
 28$vars = getVars($HTTP_GET_VARS, $HTTP_POST_VARS); 
 29
 30//class shoplist_res extends iGlobal{   
 31class shoplist_res{   
 32function shoplist_res($vars,$cookie){   
 33//$this->iGlobal($vars,$cookie);   
 34$this->outPut($vars,$cookie);   
 35} 
 36
 37//function _shoplist_res(){   
 38// $this->db->Close();   
 39//}   
 40function outPut($vars,$cookie){   
 41/*$strQuery = "select count(*) as rc from ".$this->db_c->Db_Pre."pshop order by id desc";   
 42$this->db->Query($strQuery);   
 43$RecordCount = $this->db->Assoc();   
 44$RecordCount = $RecordCount['rc'];   
 45@$this->db->freeResult();*/   
 46$RecordCount = 25;   
 47if (empty($vars['pageNo'])) $vars['pageNo']= 1;   
 48if (empty($vars['pageSize'])) $vars['pageSize']= 10;   
 49$PageCount = ceil($RecordCount/$vars['pageSize']);   
 50if ($vars['pageSize']>$RecordCount) $vars['pageSize']=$RecordCount;   
 51if ($vars['pageNo']>$PageCount) $vars['pageNo']=$PageCount;   
 52$xmlStr.= "
 53<?xml version=\"1.0\" encoding=\"GBK\" 
 54```"."\n";   
 55$xmlStr.= "<商铺列表>
 56"."\n";   
 57$xmlStr.= "\t&lt;翻页工具 pageNo=\"".$vars['pageNo']."\" pageSize=\"".$vars['pageSize']."\" total=\"".$RecordCount."\"&gt;
 58<!--翻页工具-->
 59"."\n";   
 60$xmlStr.= "\t&lt;页号码&gt;".$vars['pageNo']."
 61<!--页号码-->
 62"."\n";   
 63$xmlStr.= "\t&lt;页大小&gt;".$vars['pageSize']."
 64<!--页大小-->
 65"."\n";   
 66$xmlStr.= "\t&lt;数量&gt;".$RecordCount."
 67<!--数量-->
 68"."\n";   
 69$xmlStr.= "\t&lt;页数量&gt;".$PageCount."
 70<!--页数量-->
 71"."\n";   
 72//$strQuery = "select t1.*,t2.* from ".$this-&gt;db_c-&gt;Db_Pre."pshop as t1,".$this-&gt;db_c-&gt;Db_Pre."shoptype as t2 where t1.typenum=t2.typenum order by id desc "   
 73// ."limit ".($vars['pageNo']-1)*$vars['pageSize'].",".$vars['pageSize'];   
 74//die($strQuery);   
 75//$this-&gt;db-&gt;Query($strQuery);   
 76/*while($result = $this-&gt;db-&gt;Assoc()){   
 77$xmlStr.= "\t"."\n";   
 78$xmlStr.= "\t&lt;商铺&gt;"."\n";   
 79$xmlStr.= "\t\t&lt;名称&gt;
 80<![CDATA[".$result['shopname']."]]>
 81<!--名称-->
 82"."\n";   
 83$xmlStr.= "\t\t&lt;经营人&gt;
 84<![CDATA[".$result['shopmaster']."]]>
 85<!--经营人-->
 86"."\n";   
 87$xmlStr.= "\t\t&lt;级别&gt;
 88<![CDATA[".$result['shoptype']."]]>
 89<!--级别-->
 90"."\n";   
 91$xmlStr.= "\t\t&lt;简介&gt;
 92<![CDATA[".$result['shopintro']."]]>
 93<!--简介-->
 94"."\n";   
 95$xmlStr.= "\t\t&lt;连接地址&gt;
 96<![CDATA[personal_shop.php?id=".$result['id']."]]>
 97<!--连接地址-->
 98"."\n";   
 99$xmlStr.= "\t
100<!--商铺-->
101"."\n";   
102}*/   
103$sc= $vars['pageNo']*$vars['pageSize'];   
104if ($sc&gt;$RecordCount) $sc = $RecordCount;   
105for($i=($vars['pageNo']-1)*$vars['pageSize'];$i&lt;$sc;$i++){   
106$xmlStr.= "\t"."\n";   
107$xmlStr.= "\t&lt;商铺&gt;"."\n";   
108$xmlStr.= "\t\t&lt;名称&gt;
109<![CDATA[这是名称:".$i."]]>
110<!--名称-->
111"."\n";   
112$xmlStr.= "\t\t&lt;经营人&gt;
113<![CDATA[这是经营人:".$i."]]>
114<!--经营人-->
115"."\n";   
116$xmlStr.= "\t\t&lt;级别&gt;
117<![CDATA[这是级别:".$i."]]>
118<!--级别-->
119"."\n";   
120$xmlStr.= "\t\t&lt;简介&gt;
121<![CDATA[这是简介:".$i."]]>
122<!--简介-->
123"."\n";   
124$xmlStr.= "\t\t&lt;连接地址&gt;
125<![CDATA[personal_shop.php?id=".$i."]]>
126<!--连接地址-->
127"."\n";   
128$xmlStr.= "\t
129<!--商铺-->
130"."\n";   
131}   
132$xmlStr.= "
133<!--商铺列表-->
134"."\n";   
135header("Content-type: text/xml\n\n");   
136echo $xmlStr;   
137}   
138} 
139
140$shoplist = new shoplist_res($vars,$HTTP_COOKIE_VARS);   
141//$shoplist-&gt;_shoplist_res();   
142unset($sthoplist);   
143?&gt;
144
145  
146以上的php,注释的是我连接数据库取得的数据,既然放上来测试,也没有数据库连接,所以改了一下.偶也是刚接解xml不久,很多地方还是不很懂.见笑了......
Published At
Categories with Web编程
Tagged with
comments powered by Disqus