title: PHP / MySQL - Create array of distinct values, query db table for data associated with those values, and loop for each
body: <p>I'm not much of a PHP programmer, so I hope someone can help me with this.
  What I'm trying to do is get the distinct values from the competitor column, create an array of them, retrieve the share1-share12 values for each of those distinct values based on a number of variables, and output the competitors and their share1-12 values.</p>
  
  <p>Below is the format of my data table along with the mess of code I've been cobbling together:</p>
  
  <p>state|bigcat|competitor|metric|share1|share2|share3|share4|share5|share6|share7|share8|share9|share10|share11|share12</p>
  
  <pre><code>&lt;?php
      $product = $_GET['product'];
      $cat = $_GET['cat'];
      $state = $_GET['state'];
      $metric = $_GET['metric'];
  
      $table = $product ."_specs_states";
  
      $q = " SELECT distinct(competitor) as competitor from $table";
                  $result = $dbc-&gt;query($q) or die("unable to execute the query&lt;br /&gt;" . $dbc-&gt;errno . "&lt;br /&gt;" . $dbc-&gt;error);
                  $r = $result-&gt;fetch_array();
                  $competitors = array();
  
                  do {
                      $competitors[] = $r[0];
  
            } while ($r = $result-&gt;fetch_array());
            echo $competitors;
  
  
          foreach($competitors as $competitor){
  
  
                  $q = "SELECT * FROM $table where state = '$state' &amp;&amp; bigcat = '$cat' &amp;&amp; product = '$product' &amp;&amp; metric = '$metric' &amp;&amp; competitor = '$competitor'";
                  $result = $dbc-&gt;query($q) or die("unable to execute the query&lt;br /&gt;" . $dbc-&gt;errno . "&lt;br /&gt;" . $dbc-&gt;error);
  
                  $r = $result-&gt;fetch_array();
  
                      $share1 = ($r[5]);
                      $share2 = ($r[6]);
                      $share3 = ($r[7]);
                      $share4 = ($r[8]);
                      $share5 = ($r[9]);
                      $share6 = ($r[10]);
                      $share7 = ($r[11]);
                      $share8 = ($r[12]);
                      $share9 = ($r[13]);
                      $share10 = ($r[14]);
                      $share11 = ($r[15]);
                      $share12 = ($r[16]);
  
      }//end loop
  
      ?&gt;
  
      &lt;?php
      $i=1;
      while($i&lt;=#)
        {
      ?&gt;
  
      &lt;?= $competitor ?&gt;&lt;br /&gt;
      &lt;?= $share1 ?&gt;&lt;br /&gt;
      &lt;?= $share2 ?&gt;&lt;br /&gt;
      &lt;?= $share3 ?&gt;&lt;br /&gt;
      &lt;?= $share4 ?&gt;&lt;br /&gt;
      &lt;?= $share5 ?&gt;&lt;br /&gt;
      &lt;?= $share6 ?&gt;&lt;br /&gt;
      &lt;?= $share7 ?&gt;&lt;br /&gt;
      &lt;?= $share8 ?&gt;&lt;br /&gt;
      &lt;?= $share9 ?&gt;&lt;br /&gt;
      &lt;?= $share10 ?&gt;&lt;br /&gt;
      &lt;?= $share11 ?&gt;&lt;br /&gt;
      &lt;?= $share12 ?&gt;
  
  
      &lt;?php
        $i++;
        }
      ?&gt;
  </code></pre>