title: PHP foreach loop and separate rows with a field that has the same value
body: <p>I have a database table that has a field in the row of which can have the same value. This is a "transaction id" field that will have the order number. The rows can have the same order number but different product id's for each item ordered.</p>
  
  <p>I'm querying the database just fine and outputting the data as well. However, I want to separate the transaction id's that are the same and do something different with that data. I just can figure out how to do that for some reason.</p>
  
  <p>example:</p>
  
  <pre><code>ID        txn_id        product_id       Product_name      field5
  --------  ---------     -----------      ---------         ---------
  1         265           98               Product Name
  2         265           99               Product Name
  3         244           98               Product Name
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>So here is my updated code that still needs help. I have two queries going now. One that checks for duplicates and places them into an array, then the other that loops through the database table with an if statement which checks for matching transaction id numbers.</p>
  
  <p>First Query:</p>
  
  <pre><code>$myquery = $wpdb-&gt;get_results("SELECT txn_id , COUNT(*) FROM test_table HAVING COUNT(*) &gt; 1");
  global $TXN;                    
  foreach($myquery as $query){
      $TXN= $query-&gt;txn_id;   
  }
  </code></pre>
  
  <p>Then in the main query/loop</p>
  
  <pre><code>global $wpdb;
  $prepare_query = $wpdb-&gt;get_results("SELECT * FROM test_table" );
  
  foreach($prepare_query as $data){
      $txn_id = $data-&gt;txn_id;
      $product_id = $data-&gt;product_id;
      $product_name = $data-&gt;product_name;
      ?&gt;
      &lt;table class="shop_table my_account_orders" width="100%"&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  &lt;th align="left"&gt;Order #&lt;/th&gt;
                  &lt;th align="left"&gt;Product ID&lt;/th&gt;
                  &lt;th align="left"&gt;Product Name&lt;/th&gt;
              &lt;/tr&gt;
           &lt;/thead&gt;
  
           &lt;tbody&gt;
               &lt;tr class="order"&gt;
                   &lt;td&gt;
                       &lt;?php echo "#" .$txn_id; ?&gt;
                       &lt;?php
                          if($txn_id === $TXN){
                              echo'YES';
                          }
                        ?&gt;
                   &lt;/td&gt;
                   &lt;td&gt;
                       &lt;?php echo $product_id; ?&gt;
                   &lt;/td&gt;
                   &lt;td&gt;
                       &lt;?php echo $product_name; ?&gt;
                   &lt;/td&gt;  
               &lt;/tr&gt;
           &lt;/tbody&gt;
      &lt;/table&gt;
  &lt;?php } ?&gt;
  </code></pre>
  
  <p>This will output:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;table width="100%"&gt;
   &lt;thead&gt;
    &lt;tr&gt;
    &lt;th align="left"&gt;Order #&lt;/th&gt;
    &lt;th align="left"&gt;Product ID&lt;/th&gt;
    &lt;th align="left"&gt;Product Name&lt;/th&gt;
    &lt;/tr&gt;
   &lt;/thead&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;td&gt;#265 YES&lt;/td&gt;
     &lt;td&gt;98&lt;/td&gt;
     &lt;td&gt;Product Name&lt;/td&gt;
    &lt;/tr&gt;
   &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;table width="100%"&gt;
   &lt;thead&gt;
    &lt;tr&gt;
    &lt;th align="left"&gt;Order #&lt;/th&gt;
    &lt;th align="left"&gt;Product ID&lt;/th&gt;
    &lt;th align="left"&gt;Product Name&lt;/th&gt;
    &lt;/tr&gt;
   &lt;/thead&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;td&gt;#265 YES&lt;/td&gt;
     &lt;td&gt;99&lt;/td&gt;
     &lt;td&gt;Product Name&lt;/td&gt;
    &lt;/tr&gt;
   &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;table width="100%"&gt;
   &lt;thead&gt;
    &lt;tr&gt;
    &lt;th align="left"&gt;Order #&lt;/th&gt;
    &lt;th align="left"&gt;Product ID&lt;/th&gt;
    &lt;th align="left"&gt;Product Name&lt;/th&gt;
    &lt;/tr&gt;
   &lt;/thead&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;td&gt;#244&lt;/td&gt;
     &lt;td&gt;98&lt;/td&gt;
     &lt;td&gt;Product Name&lt;/td&gt;
    &lt;/tr&gt;
   &lt;/tbody&gt;
  &lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>When what I want to get is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;table width="100%"&gt;
   &lt;thead&gt;
    &lt;tr&gt;
    &lt;th align="left"&gt;Order #&lt;/th&gt;
    &lt;th align="left"&gt;Product ID&lt;/th&gt;
    &lt;th align="left"&gt;Product Name&lt;/th&gt;
    &lt;/tr&gt;
   &lt;/thead&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;td&gt;#265 YES&lt;/td&gt;
     &lt;td&gt;98&lt;/td&gt;
     &lt;td&gt;Product Name&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
     &lt;td&gt;#265 YES&lt;/td&gt;
     &lt;td&gt;99&lt;/td&gt;
     &lt;td&gt;Product Name&lt;/td&gt;
    &lt;/tr&gt;
   &lt;/tbody&gt;
  &lt;/table&gt;
  
  &lt;table width="100%"&gt;
   &lt;thead&gt;
    &lt;tr&gt;
    &lt;th align="left"&gt;Order #&lt;/th&gt;
    &lt;th align="left"&gt;Product ID&lt;/th&gt;
    &lt;th align="left"&gt;Product Name&lt;/th&gt;
    &lt;/tr&gt;
   &lt;/thead&gt;
   &lt;tbody&gt;
    &lt;tr&gt;
     &lt;td&gt;#244&lt;/td&gt;
     &lt;td&gt;98&lt;/td&gt;
     &lt;td&gt;Product Name&lt;/td&gt;
    &lt;/tr&gt;
   &lt;/tbody&gt;
  &lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>