<?php

// database connection info
$dbInfo = array('host' => 'localhost', 'name' => 'doctrine_website', 'user' => 'root', 'pass' => '');
// connect to the database
$db = new PDO('mysql:host=' . $dbInfo['host'] . ';dbname=' . $dbInfo['name'] . ';', $dbInfo['user'], $dbInfo['pass']);
$db->setATtribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlQuery = "SELECT\n    b.id as blog_id,\n    b.name as blog_title,\n    b.slug as blog_slug,\n    b.body as blog_body,\n    b.created_at as blog_date,\n    c.poster as comment_poster,\n    c.id as comment_id,\n    c.created_at as comment_date,\n    c.body as comment_body\nFROM comment c INNER JOIN blog_post b ON b.id = c.record_id ORDER BY b.id";
$result = $db->query($sqlQuery);
$rawdata = $result->fetchAll(PDO::FETCH_ASSOC);
// Build the comments sql for all the blog posts
$itemXml = '';
$lastId = null;
$first = true;
foreach ($rawdata as $data) {
    if ($lastId != $data['blog_id']) {
        if ($first) {
            $first = false;
        } else {
            $itemXml .= '</item>';
        }
        $itemXml .= '
    <item>
      <!-- title of article -->
      <title>' . $data['blog_title'] . '</title>
      <!-- absolute URI to article -->
      <link>http://www.doctrine-project.org/blog/' . $data['blog_slug'] . '</link>
      <!-- thread body; use cdata; html allowed (though will be formatted to DISQUS specs) -->
      <content:encoded><![CDATA[' . $data['blog_body'] . ']]></content:encoded>
      <!-- value used within disqus_identifier; usually internal identifier of article -->
      <dsq:thread_identifier>' . $data['blog_slug'] . '</dsq:thread_identifier>