xml+php动态载入与分页
这是在看太平洋网的评论时看到的,太平洋网是用jsp做为后台语言,用来产生xml文件.然后在把数据绑定到html上的.我就用php也做了一个以下是源文件.
-----------------xml.htm------------------
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>xml</title>
</head>
<body>
<xml id="shoplist" src="xml.php"></xml>
<script>
function jump(form)
{
if (form.pagenum.value>parseint(pagecount.innerhtml)) return false;
if (form.pagesize.value>parseint(recordcount.innerhtml)) return false;
shoplist.src="xml.php?pageno="+form.pagenum.value+"&pagesize="+form.pagesize.value+"&time="+(new date()).gettime();
}
function pagecount_onpropertychange(form)
{
if (form.pagenum.value>parseint(pagecount.innerhtml)) form.pagenum.value=parseint(pagecount.innerhtml);
if (form.pagesize.value>parseint(recordcount.innerhtml)) form.pagesize.value=parseint(recordcount.innerhtml);
if(form.pagenum.value==1)
{
form.firstpage.disabled=true;
form.previouspage.disabled=true;
}
else
{
form.firstpage.disabled=false;
form.previouspage.disabled=false;
}
if(form.pagenum.value==form.pagecount.value)
{
form.nextpage.disabled=true;
form.lastpage.disabled=true;
}
else
{
form.nextpage.disabled=false;
form.lastpage.disabled=false;
}
}
</script>
<table width="100%" height="25" border="0" cellpadding="2" cellspacing="1" id="shoptbl" datasrc=#shoplist datafld=商铺>
<thead>
<tr>
<td height="25" colspan="4"><span class="shop_3d_text">店铺列表</span></td>
</tr>
<tr>
<td width="30%">商铺名称</td>
<td width="20%">经营者</td>
<td width="20%">店铺级别</td>
<td width="30%">店铺简介</td>
</tr>
</thead>
<tbody>
<tr style="background-color:expression(rowindex%2==1?'#efefef':'#ffffff')">
<td><a datafld="连接地址"><span datafld="名称">名称</span></a></td>
<td><span datafld="经营人">经营人</span></td>
<td><span datafld="级别">级别</span></td>
<td><span datafld="简介">简介</span></td>
</tr>
</tbody>
</table>
<form return onsubmit="jump(this);return false">
<div align="right">
每页显示
<input name=pagesize datasrc=#shoplist datafld=页大小 size=2>条,
<input type=submit value="" style="width:0">
共<span id="pagecount" datasrc=#shoplist datafld=页数量></span>页
<span id="recordcount" datasrc=#shoplist datafld=数量 ></span>条
<input name=firstpage type=submit value=首页 onclick="pagenum.value=1">
<input name=previouspage type=submit value=上页 onclick="pagenum.value=pagenum.value-1">
第<input name=pagenum datasrc=#shoplist datafld=页号码 size=2>页
<input name=nextpage type=submit value=下页 onclick="pagenum.value=(pagenum.value-0)+1">
<input name=lastpage type=submit value=尾页 onclick="pagenum.value=pagecount.value" >
<input name=pagecount type=hidden datasrc=#shoplist datafld=页数量 onpropertychange="pagecount_onpropertychange(form)">
</div> </form>
</body>
</html>
-----------------xml.php------------------
<?php
//require_once "source/global.php";
function getvars($get_vars, $post_vars) {
$i = count($get_vars);
$j = count($post_vars);
if ($i > 0 && $j > 0 ) {
while(list($key, $val) = each($get_vars)) {
$vars[$key] = $val;
}
while(list($key2, $val2) = each($post_vars)) {
$vars[$key2] = $val2;
}
} elseif ($i > 0) {
while(list($key, $val) = each($get_vars)) {
$vars[$key] = $val;
}
} elseif ($j > 0) {
while(list($key2, $val2) = each($post_vars)) {
$vars[$key2] = $val2;
}
} else {
$vars = array();
}
return $vars;
}
$vars = getvars($http_get_vars, $http_post_vars);
//class shoplist_res extends iglobal{
class shoplist_res{
function shoplist_res($vars,$cookie){
//$this->iglobal($vars,$cookie);
$this->output($vars,$cookie);
}
//function _shoplist_res(){
// $this->db->close();
//}
function output($vars,$cookie){
/*$strquery = "select count(*) as rc from ".$this->db_c->db_pre."pshop order by id desc";
$this->db->query($strquery);
$recordcount = $this->db->assoc();
$recordcount = $recordcount['rc'];
@$this->db->freeresult();*/
$recordcount = 25;
if (empty($vars['pageno'])) $vars['pageno']= 1;
if (empty($vars['pagesize'])) $vars['pagesize']= 10;
$pagecount = ceil($recordcount/$vars['pagesize']);
if ($vars['pagesize']>$recordcount) $vars['pagesize']=$recordcount;
if ($vars['pageno']>$pagecount) $vars['pageno']=$pagecount;
$xmlstr.= "<?xml version=\"1.0\" encoding=\"gbk\" ?>"."\n";
$xmlstr.= "<商铺列表>"."\n";
$xmlstr.= "\t<翻页工具 pageno=\"".$vars['pageno']."\" pagesize=\"".$vars['pagesize']."\" total=\"".$recordcount."\"></翻页工具>"."\n";
$xmlstr.= "\t<页号码>".$vars['pageno']."</页号码>"."\n";
$xmlstr.= "\t<页大小>".$vars['pagesize']."</页大小>"."\n";
$xmlstr.= "\t<数量>".$recordcount."</数量>"."\n";
$xmlstr.= "\t<页数量>".$pagecount."</页数量>"."\n";
//$strquery = "select t1.*,t2.* from ".$this->db_c->db_pre."pshop as t1,".$this->db_c->db_pre."shoptype as t2 where t1.typenum=t2.typenum order by id desc "
// ."limit ".($vars['pageno']-1)*$vars['pagesize'].",".$vars['pagesize'];
//die($strquery);
//$this->db->query($strquery);
/*while($result = $this->db->assoc()){
$xmlstr.= "\t"."\n";
$xmlstr.= "\t<商铺>"."\n";
$xmlstr.= "\t\t<名称><![cdata[".$result['shopname']."]]></名称>"."\n";
$xmlstr.= "\t\t<经营人><![cdata[".$result['shopmaster']."]]></经营人>"."\n";
$xmlstr.= "\t\t<级别><![cdata[".$result['shoptype']."]]></级别>"."\n";
$xmlstr.= "\t\t<简介><![cdata[".$result['shopintro']."]]></简介>"."\n";
$xmlstr.= "\t\t<连接地址><![cdata[personal_shop.php?id=".$result['id']."]]></连接地址>"."\n";
$xmlstr.= "\t</商铺>"."\n";
}*/
$sc= $vars['pageno']*$vars['pagesize'];
if ($sc>$recordcount) $sc = $recordcount;
for($i=($vars['pageno']-1)*$vars['pagesize'];$i<$sc;$i++){
$xmlstr.= "\t"."\n";
$xmlstr.= "\t<商铺>"."\n";
$xmlstr.= "\t\t<名称><![cdata[这是名称:".$i."]]></名称>"."\n";
$xmlstr.= "\t\t<经营人><![cdata[这是经营人:".$i."]]></经营人>"."\n";
$xmlstr.= "\t\t<级别><![cdata[这是级别:".$i."]]></级别>"."\n";
$xmlstr.= "\t\t<简介><![cdata[这是简介:".$i."]]></简介>"."\n";
$xmlstr.= "\t\t<连接地址><![cdata[personal_shop.php?id=".$i."]]></连接地址>"."\n";
$xmlstr.= "\t</商铺>"."\n";
}
$xmlstr.= "</商铺列表>"."\n";
header("content-type: text/xml\n\n");
echo $xmlstr;
}
}
$shoplist = new shoplist_res($vars,$http_cookie_vars);
//$shoplist->_shoplist_res();
unset($sthoplist);
?>
以上的php,注释的是我连接数据库取得的数据,既然放上来测试,也没有数据库连接,所以改了一下.偶也是刚接解xml不久,很多地方还是不很懂.见笑了......