2012-05-24 47 views
2

如何生成HTML文件的數量取決於數組的長度PHP如何生成HTML文件取決於數組的長度PHP

我有一個XML文件。

<books> 
    <book x="1" y="2"> 
    <name x="5" y="12">Java</name> 
    <author x="8" y="16">Rao</author> 
    </book> 
    <book x="12" y="20"> 
    <name x="5" y="12">Php</name> 
    <author x="5" y="12">Naidu</author> 
    </book> 
    <book x="19" y="29"> 
    <name x="25" y="22">Xml</name> 
    <author x="25" y="12">Gowda</author> 
    </book> 
</books> 

我已經使用php將它轉換爲數組。

我有標準的html文件,即模板。

<body> 
    <div id="books" style="float:right; position:absolute; left: 199px; top: 245px;"> 
    <div id="book" style="float:right; position:absolute; left: 199px; top: 245px;"> 
    <div id="name" style="float:right; position:absolute; left: 199px; top: 245px;"></div> 
    <div id="author" style="float:right; position:absolute; left: 199px; top: 245px;"></div> 
    </div> 
    </div> 
</body> 

基於陣列legth,我需要生成與DIV這是目前在數組內的動態值的那些許多HTML文件。 在這裏,我需要生成3個html文件(因爲我有3本書元素)。如何通過使用數組生成html文件。

陣列是這個樣子:

Array 
(
    [book] => Array 
     (
      [0] => Array 
       (
        [name] => Java 
        [author] => Rao 
        [@attributes] => Array 
         (
          [x] => 1 
          [y] => 2 
         ) 

       ) 

      [1] => Array 
       (
        [name] => Php 
        [author] => Naidu 
        [@attributes] => Array 
         (
          [x] => 12 
          [y] => 20 
         ) 

       ) 

      [2] => Array 
       (
        [name] => Xml 
        [author] => Gowda 
        [@attributes] => Array 
         (
          [x] => 19 
          [y] => 29 
         ) 

       ) 

     ) 

) 
+1

什麼是PHP數組是什麼樣子?並且請停止使用內聯CSS。 – PeeHaa

+0

你的模板裏面包含'books' div和'book' div。所以它是整個你的xml結構的模板?所有的書都應該在你的html模板中?或者,html模板是針對一本書的,您需要爲每本書生成文件? –

+0

我需要每本書的HTML模板。不是洞頁。在整個html頁面中,我需要將這些鏈接顯示爲標籤。 – Daya

回答

0

在你的html模板中,使用實模板這樣的分隔符:

<body> 
    <div id="books" style="float:right; position:absolute; left: 199px; top: 245px;"> 
     <div id="book" style="float:right; position:absolute; left: 199px; top: 245px;"> 
      <div id="name" style="float:right; position:absolute; left: 199px; top: 245px;">{name}</div> 
      <div id="author" style="float:right; position:absolute; left: 199px; top: 245px;">{author}</div> 
     </div> 
    </div> 
</body> 

在PHP端,使用正則表達式在你的模板來替換數據:

 $html_template = file_get_contents('/path/to/your/html/template'); 

     foreach($books['book'] as $i => $book) { 
      $file_name = "html_page_for_book_{$i}"; 
      $html_file = fopen($file_name,'w'); 

      $html_data = preg_replace(array('/{author}/','/{name}/'),array($book['author'],$book['name']),$html_template); 
      fwrite($html_file,$html_data); 

      fclose($html_file); 

     } 
0

我期望誰可以解析XML到一個數組將能夠做到這一點太多,但在這裏你去反正:

<body> 
    <div id="books" style="float:right; position:absolute; left: 199px; top: 245px;"> 
     <?php foreach($books as $book) : ?> 
     <div id="book" style="float:right; position:absolute; left: <?php echo $book['@attributes']['x']; ?>px; top: <?php echo $book['@attributes']['y']; ?>px;"> 
      <div id="name" style="float:right; position:absolute; left: 199px; top: 245px;"><?php echo $book['name']; ?></div> 
      <div id="author" style="float:right; position:absolute; left: 199px; top: 245px;"><?php echo $book['author']; ?></div> 
     </div> 
     <?php endforeach; ?> 
    </div> 
</body> 

ED | IT 我爲本書添加了x/y屬性,我確定你可以自己完成其他功能

+0

書中的作者有不同的屬性 – Daya

+0

你期望在屬性中發生什麼? – MrSoundless

+0

我需要在CSS中用左側和頂部位置替換這些屬性 – Daya