title: Modified pre-order tree traversal - order children by score
body: <p>So I have been able to output the data how I want except now I want to be able to rearrange it depending on the score of individual items. So right now I output something like this:</p>
  
  <pre><code>&lt;ol&gt;
   Item1
      &lt;ol&gt;
       subitem1A
          &lt;ol&gt;
           subsubitem1A
          &lt;/ol&gt;
      &lt;/ol&gt;
      &lt;ol&gt;
       subitem1B
      &lt;/ol&gt;
  &lt;/ol&gt;
  &lt;ol&gt;
   Item2
  &lt;/ol&gt;
  </code></pre>
  
  <p>I get that by putting the left and right values in an array and iterating through it. If it is a left value, it puts out <code>&lt;ol&gt;item</code> and if it is right it closes it with <code>&lt;/ol&gt;</code></p>
  
  <p>Using the example above, what I want to do is if <code>Item2</code> has a better score than <code>Item1</code>, <code>Item2</code> will be first and <code>Item1</code> with all its children will be after it. If <code>subitem1B</code> has a better score than <code>subitem1A</code>, <code>subitem1B</code> will be before <code>subitem1A</code> and all its children.</p>
  
  <p>I'm not sure if I would have to do something with the DB query or something before running the loop that puts all the html in a variable for output, but I was thinking along the lines if I had the output somehow I could check for any <code>&lt;ol&gt;</code> that are direct children of the parent <code>&lt;ol&gt;</code>. So if I look at <code>subitem1A</code> and see its parent is <code>Item1</code> then look for any <code>&lt;ol&gt;</code> who has parent <code>Item1</code>, check the score, and then order accordingly. I don't know if that is possible but it was my thought process. </p>
  
  <p>edit - Adding more info as requested.</p>
  
  <p>I do this query:</p>
  
  <pre><code>$sql = "SELECT * FROM table WHERE uCId = '$uCId' AND lft BETWEEN 2 AND '$max' ORDER BY lft ASC";
  $result = mysql_query($sql);
  </code></pre>
  
  <p>Then:</p>
  
  <pre><code>$ol = array();
  $html = "";
  while ($row = mysql_fetch_array($result)) {          
          $ol[$row['lft']] = array('type'=&gt;'open',
                                      'text'=&gt;$row['text']
                                      //and more stuff like score, date, etc 
                                      );
          $ol[$row['rgt']]  = array('type'=&gt;'close');
      }
  $olCount = count($ol);
  for ($d=2; $d&lt;=$olCount; $d++) {
          if ($ol[$d]['type'] == 'open') {
              $html.= '&lt;ol&gt;various html'.$ol[$d]['text'].'various html';
                      }else{
                            $html .= '&lt;/ol&gt;';
                      }
  </code></pre>
  
  <p>Basically I just left out some html which would just make things harder to read. The score can be placed anywhere it would need to be. the id for the <code>&lt;ol&gt;</code> could be the score or anything. Whatever would work.</p>