title: query result grouping and user interface
body: <p>I have a database table as</p>
  
  <pre><code>course(id, name, duration);
  subject (id, course_id, elective_id, name)
  </code></pre>
  
  <p>Sample data for subject table are:</p>
  
  <pre><code>1, 100, NULL, subject1
  2, 100, NULL, subject2
  3, 100, NULL, subject3
  4, 100, 1, subject4
  5, 100, 1, subject5
  6, 100, 2, subject6
  7, 100, 2, subject7
  8, 100, 1, subject8
  9, 100, 2, subject9
  </code></pre>
  
  <p>rows having NULL are core subjects and others are electives. I want to display the format in html as following...</p>
  
  <pre><code>&lt;ul id="select"&gt;
      &lt;li&gt;subject1&lt;/li&gt;
      &lt;li&gt;subject2&lt;/li&gt;
      &lt;li&gt;subject3&lt;/li&gt;
  
      &lt;li&gt;
          &lt;ul id="elective1"&gt;
              &lt;li&gt;subject4&lt;/li&gt;
              &lt;li&gt;subject5&lt;/li&gt;
              &lt;li&gt;subject8&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
  
      &lt;li&gt;
          &lt;ul id="elective2"&gt;
              &lt;li&gt;subject6&lt;/li&gt;
              &lt;li&gt;subject7&lt;/li&gt;
              &lt;li&gt;subject9&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>This is what i have tried that does not works:</p>
  
  <pre><code>SQL = 'SELECT * FROM subject ORDER BY elective_id ASC';
  </code></pre>
  
  <p>LOGIC :</p>
  
  <pre><code>&lt;ul id="select"&gt;
  foreach($resource as $field){
    if($field-&gt;elective_id == NULL){
       echo '&lt;li&gt;'.$field-&gt;name.'&lt;/li&gt;';
    } else {
       echo '&lt;li&gt;';
       echo '&lt;ul id="'.$field-&gt;elective_id.'"&gt;';
       echo '&lt;li&gt;'.$field-&gt;name.'&lt;/li&gt;';
       echo '&lt;/ul&gt;';
       echo '&lt;/li&gt;';
    }
  }
  &lt;/ul&gt;
  </code></pre>