title: Trying to split a MYSQL query into two parts
body: <p>I'm building a dynamic HTML table for a gaming league. The problem I am having is that the table columns are only drawn when there is a score posted for that particular game. If there are no scores, say at the beginning of a tournament, there's no table at all.</p>
  
  <p>I need to split my query so that building the headers of my table from my games list happens regardless of whether there are any scores or not.</p>
  
  <pre><code>// Write query to retrieve tournament games
  $strSQL = "SELECT nfojm_djc2_items.name AS game, nfojm_djc2_items.rom AS rom, nfojm_users.username AS name, submit_score.gamescore AS score, nfojm_comprofiler.avatar AS avatar
  FROM nfojm_djc2_items, nfojm_users, submit_score, select_game, nfojm_comprofiler
  WHERE submit_score.gameID = select_game.id
  AND select_game.gamecatalogID = nfojm_djc2_items.id
  AND submit_score.userID = nfojm_users.id
  AND submit_score.userID = nfojm_comprofiler.user_id
  AND submit_score.tournID = $tournID
  ORDER BY submit_score.gamescore DESC";
  
  // Execute the query.
  $query = mysqli_query($con, $strSQL); 
  
  while ($row = mysqli_fetch_array($query)) {  
  
      if (!in_array($row["game"], $games)) {
          $i=0;
          $indexes[$row["game"]] = 0;
          $games[] = $row["game"];     
      } else {    
          $indexes[$row["game"]]++;   
          $i = $indexes[$row["game"]];
      }
  
      if (!in_array($row["rom"], $roms)) {
          $i=0;
          $indexes[$row["rom"]] = 0;
          $roms[] = $row["rom"];     
      } else {    
          $indexes[$row["rom"]]++;   
          $i = $indexes[$row["rom"]];
      }
  
      $scores[$i][$row["game"]] ="&lt;div style='float:left; margin-right:7px;'&gt;&lt;img src='http://www.arcadeicons.com/images/comprofiler/" . $row["avatar"] . "' height='35' width='35' style='border-radius:50%'&gt;&lt;/div&gt;" . $row["name"] . "&lt;br&gt;&lt;h3&gt;" . $row["score"] . "&lt;/h3&gt;";
  
  }
  // Close the connection
  mysqli_close($con);
  
  print ('&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;IMG SRC="http://www.arcadeicons.com/images/jem/venues/small/'.$loc_image.'" style="height:86px; margin-right:8px; border-radius:5%"&gt;&lt;/td&gt;');
  print ('&lt;td&gt;&lt;p style="text-align: left; margin-bottom:15px;"&gt;&lt;h1 style="color:#222222;"&gt;'.$tourn_name.'&lt;/h1&gt;&lt;/p&gt;');
  print ('&lt;p style="text-align: left;"&gt;&lt;h3&gt;'.$venue_name.' , '.$city_name.' , '.$state_name.' , '.$country_name.'&lt;/h2&gt;&lt;/p&gt;');
  print ('&lt;p style="text-align: left;"&gt;&lt;h3&gt;'.$tourn_date.'&lt;/h2&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;');
  
  
  // PUT SCORES TO SCREEN
  // GAMES
  print('&lt;table class="rounded" style="border:2px solid #999999;"&gt;&lt;thead&gt;&lt;tr&gt;');
  
  foreach ($roms as $rom) {
      print("&lt;th class='block'&gt;&lt;div style=\"width:130px;height:35px;background-image: url(http://www.arcadeicons.com/media/marquees/{$rom}1.png);background-size:cover;background-position:center center;\"&gt;&lt;/div&gt;&lt;/th&gt;");
  }
  print("&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;");
  foreach ($games as $game) {
      print("&lt;td class='block' style='text-align:center; background-color:#444444; color:white;'&gt;&lt;h4&gt;{$game}&lt;/h4&gt;&lt;/td&gt;");
  }
  
  print("&lt;/tr&gt;");
  
  foreach ($scores as $score) {
  
      print("&lt;tr&gt;");        
  
        foreach ($games as $game) {
  
          if ( isset($score[$game]) ){
              $ps = $score[$game];
          } else {
              $ps = "";
          }
  
          print("&lt;td style=\"padding-left:8px;\" class=\"block\"&gt;&lt;h4&gt;{$ps}&lt;/h4&gt;&lt;/td&gt;");
      }
  }
  
  print("&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;");
  </code></pre>
  
  <p>Results look like this when there are scores posted.</p>
  
  <p><a href="https://i.stack.imgur.com/eDAVG.png" rel="nofollow noreferrer">picture of table when rendered</a></p>