title: Compare results from two different mysqli_fetch_array with PHP
body: <p>I created a DB where I am saving information about apps. Those apps can be linked to a <em>vertical</em>. I designed the DB in such way that I have one table for standard information about each application which includes an auto-incremented ID a name etc. I have another table that lists all different industries/verticals.</p>
  
  <p>Except for those 2 tables, I have a third one connecting the two, which looks like this (I'm showing it in a PHP array because I think it's easier to read):</p>
  
  <pre><code>  $verticals_joint = array(
    array('vertical_joint_id' =&gt; '1','vertical_id' =&gt; '11','demo_id' =&gt; '173'),
    array('vertical_joint_id' =&gt; '2','vertical_id' =&gt; '12','demo_id' =&gt; '173'),
    array('vertical_joint_id' =&gt; '3','vertical_id' =&gt; '13','demo_id' =&gt; '173'),
    array('vertical_joint_id' =&gt; '4','vertical_id' =&gt; '2','demo_id' =&gt; '174'),
    array('vertical_joint_id' =&gt; '5','vertical_id' =&gt; '4','demo_id' =&gt; '174'),
    array('vertical_joint_id' =&gt; '6','vertical_id' =&gt; '3','demo_id' =&gt; '175'),
    array('vertical_joint_id' =&gt; '7','vertical_id' =&gt; '4','demo_id' =&gt; '175'),
    array('vertical_joint_id' =&gt; '8','vertical_id' =&gt; '8','demo_id' =&gt; '176'),
    array('vertical_joint_id' =&gt; '9','vertical_id' =&gt; '9','demo_id' =&gt; '176'),
    array('vertical_joint_id' =&gt; '10','vertical_id' =&gt; '1','demo_id' =&gt; '92'),
    array('vertical_joint_id' =&gt; '11','vertical_id' =&gt; '3','demo_id' =&gt; '92'),
    array('vertical_joint_id' =&gt; '12','vertical_id' =&gt; '12','demo_id' =&gt; '92'),
    array('vertical_joint_id' =&gt; '13','vertical_id' =&gt; '1','demo_id' =&gt; '179'),
    array('vertical_joint_id' =&gt; '14','vertical_id' =&gt; '3','demo_id' =&gt; '179'),
    array('vertical_joint_id' =&gt; '15','vertical_id' =&gt; '4','demo_id' =&gt; '179'),
    array('vertical_joint_id' =&gt; '16','vertical_id' =&gt; '2','demo_id' =&gt; '180'),
    array('vertical_joint_id' =&gt; '17','vertical_id' =&gt; '4','demo_id' =&gt; '180')
  );
  </code></pre>
  
  <p>The vertical_id connects the industry/vertical with the application/demo. The demo_id references the application (or demo as I called it). </p>
  
  <p>Now comes my issue. I came up with a while-loop in PHP and I nested an if-statement into it so I can display all verticals and additionally mark which verticals the given application was created for. E.g. this is what I want to achieve:</p>
  
  <pre><code>&lt;select id="Vertical[]" class="form-control verticalText" multiple="multiple"&gt;
  &lt;option value="1"&gt;Education&lt;/option&gt;
  &lt;option value="2"&gt;Manufacturing&lt;/option&gt;
  &lt;option value="3"&gt;State, Local or Provincial Government&lt;/option&gt;
  &lt;option value="4"&gt;Federal Government&lt;/option&gt;
  &lt;option value="5"&gt;Other&lt;/option&gt;
  &lt;option value="6"&gt;Transportation and Logistics&lt;/option&gt;
  &lt;option value="7"&gt;Healthcare&lt;/option&gt;
  &lt;option value="8"&gt;Petrochemical&lt;/option&gt;
  &lt;option value="9"&gt;Utilities&lt;/option&gt;
  &lt;option value="10"&gt;Public Safety&lt;/option&gt;
  &lt;option value="11" selected&gt;Hospitality&lt;/option&gt;
  &lt;option value="12" selected&gt;Wholesale Distribution&lt;/option&gt;
  &lt;option value="13" selected&gt;Human Services&lt;/option&gt;
  &lt;option value="14"&gt;Retail&lt;/option&gt;    
  </code></pre>
  
  <p></p>
  
  <p>That's what I want to achieve but instead when I use my while loop it only correctly selects <em>Hospitality</em> and doesn't select <em>Wholesale Dist</em> or <em>Human Services</em>.</p>
  
  <p>My while-loop + nested if-statement look like this:</p>
  
  <pre><code>//Gathers the names of all existing verticals.
  $vertical_query  = "SELECT * FROM demos.verticals";
  $result_vertical = mysqli_query($con, $vertical_query);
  //Gathers information about the particular application you are comparing with. 
  //It retrieves the verticals for a particular app/demo id. 
  //For this example I used id 173
  $joint_query  = "SELECT o.vertical_id, o.vertical_name FROM demos.alldemos alld
      LEFT JOIN demos.verticals_joint j ON alld.id = j.demo_id
      LEFT JOIN demos.verticals o ON o.vertical_id = j.vertical_id
      WHERE alld.id = 173";
  $result_joint = mysqli_query($con, $joint_query);
  $compare      = mysqli_fetch_array($result_joint);
  $output       = '';
  // while there are Verticals to be listed.
  while ($row_vertical = mysqli_fetch_array($result_vertical)) {
      // If a vertical is already selected for this particular app id,        
      if ($compare['vertical_name'] == $row_vertical["vertical_name"]) {
          //then add the word select to the option so it will be selected
          $output .= '&lt;option value="'.$row_vertical["vertical_id"].'" selected&gt;'.$row_vertical['vertical_name'].'&lt;/option&gt;';
      } elseif ($compare['vertical_name'] != $row_vertical["vertical_name"]) {
          //Otherwise just print out the normal option without 'selected'.
          $output .= '&lt;option value="'.$row_vertical["vertical_id"].'"&gt;'.$row_vertical["vertical_name"].'&lt;/option&gt;';
      }
  }
  echo $output;
  </code></pre>
  
  <p>Would you know why it only compares the first item from my SQL join against the verticals from my other SELECT query? Would you suggest to do things differently?</p>