title: PHP MySQL Generate Multiple xml Sitemap index
body: <p>I have this PHP class for generate dynamic xml sitemap:</p>
  
  <p>class::</p>
  
  <pre><code>namespace SitemapPHP;
  class Sitemap {
      /**
       *
       * @var \XMLWriter
       */
      private $writer;
      private $domain;
      private $path;
      private $filename = 'sitemap';
      private $current_item = 0;
      private $current_sitemap = 0;
      const EXT = '.xml';
      const SCHEMA = 'http://www.sitemaps.org/schemas/sitemap/0.9';
      const DEFAULT_PRIORITY = 0.5;
      const ITEM_PER_SITEMAP = 50000;
      const SEPERATOR = '-';
      const INDEX_SUFFIX = 'index';
      /**
       *
       * @param string $domain
       */
      public function __construct($domain) {
          $this-&gt;setDomain($domain);
      }
      /**
       * Sets root path of the website, starting with http:// or https://
       *
       * @param string $domain
       */
      public function setDomain($domain) {
          $this-&gt;domain = $domain;
          return $this;
      }
      /**
       * Returns root path of the website
       *
       * @return string
       */
      private function getDomain() {
          return $this-&gt;domain;
      }
      /**
       * Returns XMLWriter object instance
       *
       * @return \XMLWriter
       */
      private function getWriter() {
          return $this-&gt;writer;
      }
      /**
       * Assigns XMLWriter object instance
       *
       * @param \XMLWriter $writer 
       */
      private function setWriter(\XMLWriter $writer) {
          $this-&gt;writer = $writer;
      }
      /**
       * Returns path of sitemaps
       * 
       * @return string
       */
      private function getPath() {
          return $this-&gt;path;
      }
      /**
       * Sets paths of sitemaps
       * 
       * @param string $path
       * @return Sitemap
       */
      public function setPath($path) {
          $this-&gt;path = $path;
          return $this;
      }
      /**
       * Returns filename of sitemap file
       * 
       * @return string
       */
      private function getFilename() {
          return $this-&gt;filename;
      }
      /**
       * Sets filename of sitemap file
       * 
       * @param string $filename
       * @return Sitemap
       */
      public function setFilename($filename) {
          $this-&gt;filename = $filename;
          return $this;
      }
      /**
       * Returns current item count
       *
       * @return int
       */
      private function getCurrentItem() {
          return $this-&gt;current_item;
      }
      /**
       * Increases item counter
       * 
       */
      private function incCurrentItem() {
          $this-&gt;current_item = $this-&gt;current_item + 1;
      }
      /**
       * Returns current sitemap file count
       *
       * @return int
       */
      private function getCurrentSitemap() {
          return $this-&gt;current_sitemap;
      }
      /**
       * Increases sitemap file count
       * 
       */
      private function incCurrentSitemap() {
          $this-&gt;current_sitemap = $this-&gt;current_sitemap + 1;
      }
      /**
       * Prepares sitemap XML document
       * 
       */
      private function startSitemap() {
          $this-&gt;setWriter(new \XMLWriter());
          if ($this-&gt;getCurrentSitemap()) {
              $this-&gt;getWriter()-&gt;openURI($this-&gt;getPath() . $this-&gt;getFilename() . self::SEPERATOR . $this-&gt;getCurrentSitemap() . self::EXT);
          } else {
              $this-&gt;getWriter()-&gt;openURI($this-&gt;getPath() . $this-&gt;getFilename() . self::EXT);
          }
          $this-&gt;getWriter()-&gt;startDocument('1.0', 'UTF-8');
          $this-&gt;getWriter()-&gt;setIndent(true);
          $this-&gt;getWriter()-&gt;startElement('urlset');
          $this-&gt;getWriter()-&gt;writeAttribute('xmlns', self::SCHEMA);
      }
      /**
       * Adds an item to sitemap
       *
       * @param string $loc URL of the page. This value must be less than 2,048 characters. 
       * @param string|null $priority The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0.
       * @param string|null $changefreq How frequently the page is likely to change. Valid values are always, hourly, daily, weekly, monthly, yearly and never.
       * @param string|int|null $lastmod The date of last modification of url. Unix timestamp or any English textual datetime description.
       * @return Sitemap
       */
      public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = NULL, $lastmod = NULL) {
          if (($this-&gt;getCurrentItem() % self::ITEM_PER_SITEMAP) == 0) {
              if ($this-&gt;getWriter() instanceof \XMLWriter) {
                  $this-&gt;endSitemap();
              }
              $this-&gt;startSitemap();
              $this-&gt;incCurrentSitemap();
          }
          $this-&gt;incCurrentItem();
          $this-&gt;getWriter()-&gt;startElement('url');
          $this-&gt;getWriter()-&gt;writeElement('loc', $this-&gt;getDomain() . $loc);
          if($priority !== null)
              $this-&gt;getWriter()-&gt;writeElement('priority', $priority);
          if ($changefreq)
              $this-&gt;getWriter()-&gt;writeElement('changefreq', $changefreq);
          if ($lastmod)
              $this-&gt;getWriter()-&gt;writeElement('lastmod', $this-&gt;getLastModifiedDate($lastmod));
          $this-&gt;getWriter()-&gt;endElement();
          return $this;
      }
      /**
       * Prepares given date for sitemap
       *
       * @param string $date Unix timestamp or any English textual datetime description
       * @return string Year-Month-Day formatted date.
       */
      private function getLastModifiedDate($date) {
          if (ctype_digit($date)) {
              return date('Y-m-d', $date);
          } else {
              $date = strtotime($date);
              return date('Y-m-d', $date);
          }
      }
      /**
       * Finalizes tags of sitemap XML document.
       *
       */
      private function endSitemap() {
          if (!$this-&gt;getWriter()) {
              $this-&gt;startSitemap();
          }
          $this-&gt;getWriter()-&gt;endElement();
          $this-&gt;getWriter()-&gt;endDocument();
      }
      /**
       * Writes Google sitemap index for generated sitemap files
       *
       * @param string $loc Accessible URL path of sitemaps
       * @param string|int $lastmod The date of last modification of sitemap. Unix timestamp or any English textual datetime description.
       */
      public function createSitemapIndex($loc, $lastmod = 'Today') {
          $this-&gt;endSitemap();
          $indexwriter = new \XMLWriter();
          $indexwriter-&gt;openURI($this-&gt;getPath() . $this-&gt;getFilename() . self::SEPERATOR . self::INDEX_SUFFIX . self::EXT);
          $indexwriter-&gt;startDocument('1.0', 'UTF-8');
          $indexwriter-&gt;setIndent(true);
          $indexwriter-&gt;startElement('sitemapindex');
          $indexwriter-&gt;writeAttribute('xmlns', self::SCHEMA);
          for ($index = 0; $index &lt; $this-&gt;getCurrentSitemap(); $index++) {
              $indexwriter-&gt;startElement('sitemap');
              $indexwriter-&gt;writeElement('loc', $loc . $this-&gt;getFilename() . ($index ? self::SEPERATOR . $index : '') . self::EXT);
              $indexwriter-&gt;writeElement('lastmod', $this-&gt;getLastModifiedDate($lastmod));
              $indexwriter-&gt;endElement();
          }
          $indexwriter-&gt;endElement();
          $indexwriter-&gt;endDocument();
      }
  }
  </code></pre>
  
  <p>I can add item using:
  <code>$sitemap-&gt;addItem('/', '1.0', 'daily', 'Today');</code>
  and add sitemapindex using: <code>$sitemap-&gt;createSitemapIndex('http://example.com/sitemap/', 'Today');</code> Now I need to generate multiple sitemap and dynamic with MySQL database for each month archive:</p>
  
  <pre><code>$sql = "SELECT YEAR(FROM_UNIXTIME(timestamp)) AS YEAR, 
                  MONTH(FROM_UNIXTIME(timestamp)) AS MONTH 
           FROM ".NEWS_ARTICLES." GROUP BY YEAR, MONTH ORDER BY YEAR DESC, MONTH ";
  $newsdata = DataAccess::Fetch($sql);
  
  $currentYear = null;
  
  foreach($newsdata AS $news){            
      $sitemap-&gt;setFilename('posts-.'.$news['MONTH'].'-'.$news['YEAR'].'');
      $sitemap-&gt;addItem('/post/' . $news['slug'], '0.6', 'weekly', $post['created_at']);
  }
  </code></pre>
  
  <p>But in action I see one file and result data. How do can I generate one <code>xml</code> file for each month and put/insert data to this file (example:<code>posts-6-2016.xml</code> <code>posts-7-2016.xml</code>)?!</p>