title: Sorting MySQL results based on it's context
body: <p>I'm trying to create a nested comment system using PHP and MySQL. But I'm stuck</p>
  
  <ul>
  <li>My Database structure is id, body, time, reply and depth. </li>
  <li>A regular comment's <code>reply</code> field will be '0'. If it's replying to another it will correspond to the <code>id</code> of the comment it's replying to.</li>
  <li><code>depth</code> means how deep it is from the highest parent</li>
  </ul>
  
  <p>So if this is the contents of my table...</p>
  
  <pre><code>+------+-------------+--------+---------+---------+
  |  id  |  body       |  time  |  reply  |  depth  |
  +------+-------------+--------+---------+---------+
  |   1  |  Some msg1  |   1    |    0    |    0    |
  |   2  |  Some msg2  |   2    |    0    |    0    |
  |   3  |  aReply1    |   3    |    1    |    1    |
  |   4  |  aReply2    |   4    |    1    |    1    |
  |   5  |  aReply21   |   5    |    3    |    2    |
  +------+-------------+--------+---------+---------+
  </code></pre>
  
  <p>It would appear as something like this...</p>
  
  <pre><code>- (1) Some msg1
  -- (3) aReply1
  --- (5) aReply21
  -- (4) aReply2
  - (2) Some msg2
  </code></pre>
  
  <p>I hope it's possible using this method, it kind of goes beyond my logic.</p>