Home > php教程 > PHP源码 > body text

把分类数据转化成可读的XML文件方便调用

PHP中文网
Release: 2016-05-25 17:10:43
Original
1275 people have browsed it

一段把分类数据动态转换成XML文件的PHP代码,数据结构(id,pid,rank,category),其中pid为父ID(第一级为0),rank为等级(0为第一级,1级第二级,依此类推),category为分类名称,理论上支持无限分级转化。

<?php
 error_reporting(E_ALL ^ E_NOTICE);
 header("Content-type:text/xml"); print("<?xml version="1.0"encoding="gb2312"?>"); 
include("../include/mysql.class.php");
$id=(isset($_GET[id]))?$_GET[id]:0;
 print("<tree id=&#39;".$id."&#39;>");
 show_item(&#39;table_name&#39;,0); //table_name是你的分类表名
print("</tree>");

 function show_item($database,$pid)
{
 $db = new mysql();
 $query="select * from $database where parent=&#39;$pid&#39;";
$result=$db->query($query);
while($r=$db->fetch_array($result))
{
 print("<item id=&#39;".$r[id]."&#39; text=&#39;".$r[category]."&#39;><userdata name=&#39;rank&#39;>".$r[rank]."</userdata><userdata name=&#39;category&#39;>".$r[category]."</userdata>");
 if($r[rank]!=&#39;3&#39;)show_item($database,$r[id]); //0:第一级,3:第四级,依此类推
print("</item>");
}
$db->close();
}
?>
Copy after login

                   


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template