add multiple data in xml file using PHP xmlwriter

后端 未结 2 510
借酒劲吻你
借酒劲吻你 2021-01-14 09:07

I need to add article for each month in xml file using PHP xmlwriter:

$sql = \"SELECT *,YEAR(FROM_UNIXTIME(timestamp)) AS YEAR, 
           


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-14 09:13

    You are overwriting your XML with each loop. Consider starting document with root and closing document outside the foreach loop:

    $writer->openURI('./cache/xmls/posts-'.$news['MONTH'].'-'.$news['YEAR'].'.xml');
    $writer->startDocument('1.0','UTF-8'); 
    $writer->setIndent(4); 
    $writer->startElement('urlset');
    
    foreach ($newsdata as $news){    
      $writer->startElement('url'); 
      $writer->writeElement('loc',$news['title']); 
      $writer->endElement();
    }
    
    $writer->endElement(); 
    $writer->endDocument(); 
    $writer->flush();
    

提交回复
热议问题