title: PHP loop through 2 tables based position id and date
body: <p>I am trying to loop through 2 tables (promotion and article).
  The problem here is the promotion table. I want to display the promotion on a specific position. Eg. position 1 is the first position before the 1st article. And the article is ordered by newest date.</p>
  
  <p>Table: promotion</p>
  
  <pre><code>+----+-------------+---------+------+---------------------+
  | id | promo       | postion | user | created_date        |
  +----+-------------+---------+------+---------------------+
  | 1  | promo 1     | 1       | 1    | 2015-10-01 00:00:00 |
  +----+-------------+---------+------+---------------------+
  | 2  | promo 2     | 4       | 1    | 2015-10-01 00:00:00 |
  +----+-------------+---------+------+---------------------+
  | 3  | promo 3     | 6       | 1    | 2015-10-01 00:00:00 |
  +----+-------------+---------+------+---------------------+
  </code></pre>
  
  <p>Table: article</p>
  
  <pre><code>+----+---------------+------+---------------------+--------+
  | id | article       | user | created_date        | public |
  +----+---------------+------+---------------------+--------+
  | 1  | article 1     | 1    | 2015-10-01 00:00:00 | 1      |
  +----+---------------+------+---------------------+--------+
  | 2  | article 2     | 1    | 2015-10-02 00:00:00 | 1      |
  +----+---------------+------+---------------------+--------+
  | 3  | article 3     | 1    | 2015-10-03 00:00:00 | 1      |
  +----+---------------+------+---------------------+--------+
  | 4  | article 4     | 1    | 2015-10-04 00:00:00 | 1      |
  +----+---------------+------+---------------------+--------+
  | 5  | article 5     | 1    | 2015-10-05 00:00:00 | 1      |
  +----+---------------+------+---------------------+--------+
  | 6  | article 6     | 1    | 2015-10-06 00:00:00 | 0      |
  +----+---------------+------+---------------------+--------+
  | 7  | article 7     | 1    | 2015-10-06 00:00:00 | 1      |
  +----+---------------+------+---------------------+--------+
  | 8  | article 8     | 1    | 2015-10-06 00:00:00 | 1      |
  +----+---------------+------+---------------------+--------+
  | 9  | article 9     | 1    | 2015-10-06 00:00:00 | 1      |
  +----+---------------+------+---------------------+--------+
  </code></pre>
  
  <p>Should look like this</p>
  
  <pre><code>&lt;div&gt;Promo 1&lt;/div&gt;
      &lt;div&gt;Article 1&lt;/div&gt;
      &lt;div&gt;Article 2&lt;/div&gt;
      &lt;div&gt;Article 3&lt;/div&gt;
  &lt;div&gt;Promo 2&lt;/div&gt;
      &lt;div&gt;Article 4&lt;/div&gt;
      &lt;div&gt;Article 5&lt;/div&gt;
  &lt;div&gt;Promo 3&lt;/div&gt;
      &lt;div&gt;Article 7&lt;/div&gt;
      &lt;div&gt;Article 8&lt;/div&gt;
      &lt;div&gt;Article 9&lt;/div&gt;
  </code></pre>
  
  <p>So far I have this to display the article table</p>
  
  <pre><code>echo '&lt;table&gt;';
  
  $dbList = dblink();
  $sql = $dbList-&gt;prepare("SELECT article.* FROM article WHERE article.public = 1 ORDER BY date_created DESC");
  $sql-&gt;execute();
  while ($row = $sql-&gt;fetch()){
  
      echo '
          &lt;tr&gt;
              &lt;td width="50"&gt;
                  '.$row['id'].'
              &lt;/td&gt;
              &lt;td&gt;
                  '.$row['article'].'
              &lt;/td&gt;
              &lt;td&gt;
                  '.$row['date_created'].'
              &lt;/td&gt;
              &lt;td width="20" style="text-align: center;"&gt;
                  '.$row['public'].'
              &lt;/td&gt;
          &lt;/tr&gt;';
  
  }
  
  echo '&lt;/table&gt;';
  </code></pre>