title: how to get all the rows of a table that are in a certain category, then display a field from each row, but divide them up by subcategories
body: <p>I am working on a website that displays domain names for sale that have all been categorized into one of about 35 main categories.  Each domain name for sale is also in a subcategory of that main category.  There are between 1 and 10 subcategories of each main category.</p>
  
  <p>Presently the site just lists all the domain names in the database in the main category selected.  (It actually shows all the names in the first subcategory of the selected main category, then all the rest of the subcategory domain names in that main category).</p>
  
  <p>There is only one table in the database.  It has 4 columns and is about 
  10,000 rows long with about 35 main categories and several subcategories 
  in each main category.  It is all text except for the id (I think).  </p>
  
  <p>Here is a portion of the table:</p>
  
  <pre class="lang-none prettyprint-override"><code>Id   MainCat  SubCat           DomainName
  1    Art      Art Companies    ArtCompanies.com
  2    Beauty   Beuauty Care     CareBeauty.com
  3    Art      Art Galleries    ArtInAmerica.com
  4    Art      Art Companies    ArtInChicago.com
  5    Art      Art Galleries    ArtMuseumsInLA.com
  6    Beauty   Beauty Products  BeautyIdeas.com
  7    Art      Artists          ArtMuseumsNY.com
  8    Business Trademarks       TrademarksUSA.com
  </code></pre>
  
  <p>Following is a simplified version of the php/mysql code that presently retrieves all the domain name in a selected main category and the first subcategory in that main category.  This is the code for the top part of the text below the "keyboard" images:</p>
  
  <pre><code>$res_sub = $mysqliConnection-&gt;query("SELECT * FROM {$table} WHERE MainCategory = '$cellMainCat' AND SubCategory = '$cellSubCat' ORDER BY SubCategory ASC") or die(mysqli_error());
  echo "You are viewing &lt;div class='catFormat'&gt;$cellSubCat&lt;/div&gt; in &lt;div class='catFormat'&gt;$cellMainCat&lt;/div&gt;";
  
  /* determine number of rows in result set */
  $total_number_of_rows_returned = $res_sub-&gt;num_rows;
  $number_of_rows_remaining = 0;
  
  while ($number_of_rows_remaining &lt; ($total_number_of_rows_returned/2) &amp;&amp; (boolean) ($row = mysqli_fetch_object($res_sub))) 
  {
  $number_of_rows_remaining++;  // increment 
      echo $row-&gt;DomainName;
  }
  /* now doing to do the RIGHT SIDE MIDDLE div and put the right side results in.  We don't need to do the count half thing cause there should only be half left */
  
  while ($row = mysqli_fetch_object($res_sub)) 
  {
  echo $row-&gt;DomainName;
      echo "&lt;br&gt;";
  }
  </code></pre>
  
  <hr>
  
  <p>Here is a link to the site the way it is now:<br>
  <a href="http://bestmarketingnames.com/Tbanneredit_v40.php" rel="nofollow">http://bestmarketingnames.com/Tbanneredit_v40.php</a></p>
  
  <p><strong>My question:</strong>  I would like to modify the site so that when the user clicks the button to choose a main category, the page displays all the domain names in that main category, but broken down by subcategory. </p>
  
  <p>So for example if the person clicks the main category button of "Art", the page would display:</p>
  
  <pre class="lang-none prettyprint-override"><code>You have chosen the Main Category Art which has 8 Subcategories.  
  
  Subcategory 1 of 8 is:  Art Galleries.  The domain names for sale for 
  this subcategory are:
               myartgalleries.com
               bigartgalleries.com
               superartgaleries.com
  
  Subcategory 2 of 8 is:  Art Companies.  The domain names for sale for 
  this subcategory are:
              bigartcompanies.com
              superartcompanies.com
  
         [etc. - end example]
  </code></pre>
  
  <p>What I want to know is what code to use to cycle through the subcategories and do all the displaying one subcategory at a time as in the example above.  If I can get away without a query for every single subcategory that would be great, but I don't know enough about php/mysql to know if that's possible.  I'm okay with arrays etc.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Thanks.</p>