Home >php教程 >PHP源码 >把分类数据转化成可读的XML文件方便调用

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

PHP中文网
PHP中文网Original
2016-05-25 17:10:431308browse

一段把分类数据动态转换成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();
}
?>

                   


Statement:
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