title: How to use Twig loop value in an included SQL query
body: <p>I am trying to create an online forum with multiple forum categories. I have made a overview with all the categories, and under the name of each category, I want to link to the latest post in the mentioned category, something like this:</p>
  
  <blockquote>
    <p><strong>MY FORUM</strong> </p>
    
    <p><strong>Cool category</strong><br> Latest post: Test thread by John Doe</p>
    
    <p><strong>Interesting category</strong><br> Latest post: Another test thread by Jane Doe</p>
    
    <p><strong>Funny category</strong><br> Latest post: Jokes thread by John Doe</p>
  </blockquote>
  
  <p><br>So, I have made a SQL query to fetch all the forum categories:</p>
  
  <pre><code>$forumCategories = DB::run('SELECT * FROM forumCategories ORDER BY id ASC')-&gt;fetchAll();
  </code></pre>
  
  <p><br>And turned it into a global variable:</p>
  
  <pre><code>$twig-&gt;addGlobal('forumCategories', $forumCategories);
  </code></pre>
  
  <p><br>And made a for loop to list all the categories:</p>
  
  <pre><code>{% for category in forumCategories %}
      &lt;h3&gt;{{category.title}}&lt;/h3&gt;
  {% endfor %}
  </code></pre>
  
  <p><br>So far, so good.</p>
  
  <p>But now, I want to show the latest posts in each category. I want to run a SQL query and fetch the latest post from the table forumPosts, for each forum category, and show it inside the loop. I need to show a row from forumPosts where category is the same as category.id in the loop. How do I do this?</p>
  
  <hr>
  
  <p>Database structure and content:</p>
  
  <pre><code>CREATE TABLE `forumCategories` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `title` varchar(255) NOT NULL,
    `urlTitle` varchar(255) NOT NULL,
    `description` text NOT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  
  INSERT INTO `forumCategories` (`id`, `title`, `urlTitle`, `description`)
  VALUES
      (1,'Hovedrommet','hovedrommet','Dette er hovedrommet på Nye Dofo.'),
      (2,'Forumleker','forumleker','Her kan man leke ulike forumleker.');
  
  CREATE TABLE `forumPosts` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `title` varchar(255) NOT NULL,
    `urlTitle` varchar(255) NOT NULL,
    `category` int(11) NOT NULL,
    `content` text NOT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  
  INSERT INTO `forumPosts` (`id`, `title`, `urlTitle`, `category`, `content`)
  VALUES
      (1,'Test','test',1,'Dette er test1.'),
      (2,'Test2','test2',1,'Dette er test2.'),
      (3,'Test3','test3',2,'Dette er test3.'),
      (4,'Test4','test4',2,'Dette er test4.');
  </code></pre>
  
  <p>The output I want is a loop with "title" of all categories, and "title" of the latest post (the one with highest ID) in each category. I manage to get the category name, but not the latest post.</p>
  
  <p>I want the following output:</p>
  
  <p>Hovedrommet<br>
  Latest post: Test2</p>
  
  <p>Forumleker<br>
  Latest post: Test4</p>