php - Sorting nested While() Loops -


i'm able sort second tier while loop obvious reasons cannot first 1 sort. know cause "for" loop incrementing. want alphabetically sort first while loop second asc...any suggestions? here's code

function get_content() {     $sql1 = "select * category";     $res1 = mysql_query($sql1) or die(mysql_error());     $total = mysql_num_rows($res1) or die(mysql_error());      for($a = 1; $a <= $total; $a++) {         $sql = "select * weblinks inner join category on category_weblinks = id_category id_category = '$a' , status_weblinks = 'checked' order title_weblinks asc";         $res = mysql_query($sql) or die(mysql_error());         while($row = mysql_fetch_assoc($res)) {             echo "\n\n\n" . '<div class="post">' . "\n";             echo '<div class="title">' . "\n";             echo '<h2><a name="' . $row['shortcut_category'] . '">' . $row['title_category'] . '</a></h2>' . "\n";             echo '<p><small>posted joe <a href="mailto:"> email</a></small></p>';             echo '</div>' . "\n";             echo '<div class="entry">' . "\n";             $res = mysql_query($sql) or die(mysql_error());             while($row = mysql_fetch_assoc($res)) {                 echo "\n" . '<p><b>' .$row['title_weblinks']. '</b><br>' . "\n";                 echo $row['description_weblinks']. '<br>' . "\n";                 echo 'link: <a href="' .$row['link_weblinks']. '" target="_blank">' .$row['link_weblinks']. '</a><br>' . "\n";                 echo 'user: ' .$row['username_weblinks']. ' | password: ' .$row['password_weblinks']. '</p>' . "\n";             }             echo '<p class="links"> <a href="#" class="more">back top</a></p>';             echo '</div>';             echo '</div>';         }     } }