PHP 列出XML树(笨人笨法)

今天要做一个影视列表,后来憋出来了一段代码,倒是蛮简单的,只要逻辑结构想明白就OK了,执行效率没有进行测试,如果大家有更好的方法也请告诉我,如标题说言笨人笨法。
 
— 表的结构 `videolist`
————————————————————————————————-
CREATE TABLE `videolist` (
  `video_id` int(255) NOT NULL auto_increment,
  `video_class` varchar(255) collate utf8_unicode_ci NOT NULL,
  `video_name` varchar(255) collate utf8_unicode_ci NOT NULL,
  `video_path` varchar(255) collate utf8_unicode_ci NOT NULL,
  `video_title` varchar(255) collate utf8_unicode_ci NOT NULL,
  `video_auto` tinyint(1) NOT NULL,
  `video_buffer` int(10) NOT NULL,
  `video_abetnum` int(255) NOT NULL,
  `video_errornum` int(255) NOT NULL,
  PRIMARY KEY  (`video_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=12 ;
———————————————————————————————
 
–代码
———————————————————————————————
<?php  include  ‘connect/connect.inc’   ?>
<?php
 header("Content-Type: text/xml");
 echo ‘<?xml version="1.0" encoding="gb2312" ?>’;
 echo ‘<node label="购房影视列表">’;
?>
<?php
     $num = 0;
     //First Level
     $sql = "SELECT video_class FROM video_data ";
     mysql_select_db($dbname,$connection)or die("选择数据库出错,可能是您指定的数据库不存在!");
     $result = mysql_query($sql,$connection);
     while($row = mysql_fetch_array($result,MYSQL_NUM)){
     $class = $row[0];
     echo ‘<node label="’;
     echo $class;
     echo ‘">’;
          //Second Level
          $sql1 = "SELECT DISTINCT video_name FROM videolist WHERE video_class=’".$class."’";
          mysql_select_db($dbname,$connection)or die("选择数据库出错,可能是您指定的数据库不存在!");
          $result1 = mysql_query($sql1,$connection);
          while($row1 = mysql_fetch_array($result1,MYSQL_NUM)){
          $name = $row1[0];
          echo ‘<node label="’;
          echo $name;
          echo ‘">’;
               //Three Level
              $sql2 = "SELECT video_id,video_title FROM videolist WHERE video_name=’".$name."’ AND          video_class=’".$class."’";
              mysql_select_db($dbname,$connection)or die("’选择数据库出错,可能是您指定的数据库不存在!");
              $result2 = mysql_query($sql2,$connection);
              while($row2 = mysql_fetch_array($result2,MYSQL_NUM)){
              $data = $row2[0];
              $title = $row2[1];
              echo ‘<node label="’;
              echo $title;
              echo ‘" data="’;
              echo $data;
              echo ‘">’;
              echo ‘</node>’;
              }//End Three Level
         echo ‘</node>’;
         }//End Second Level
    echo ‘</node>’;
    }//End First Level
?>
<?php
 echo ‘</node>’;
?>
 ——————————————————————————————–
Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: