Foreach loop to generate HTML table with title in first cell of each row in PHP I've used quite a lot of foreach loops to achieve this type of HTML table that has titles in the first cell of each row:

 &lt;table&gt;
     &lt;thead&gt;
        &lt;tr&gt;
         &lt;th&gt;&lt;/th&gt;&lt;th&gt;&lt;img src='img.jpg'&gt;Product 1&lt;/th&gt;&lt;th&gt;&lt;img src='img.jpg'&gt;Product 2&lt;/th&gt;
        &lt;/tr&gt;
     &lt;/thead&gt;
     &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;Width&lt;/td&gt;&lt;td&gt;50mm&lt;/td&gt;&lt;td&gt;90mm&lt;/td&gt;
          &lt;td&gt;Material&lt;/td&gt;Plastic&lt;td&gt;&lt;/td&gt;&lt;td&gt;Gold&lt;/td&gt;
          &lt;td&gt;Diameter&lt;/td&gt;60 mm&lt;td&gt;&lt;/td&gt;&lt;td&gt;70mm&lt;/td&gt;
          &lt;td&gt;Battery Hours&lt;/td&gt;60 hours&lt;td&gt;&lt;/td&gt;&lt;td&gt;50 hours&lt;/td&gt;
        &lt;/tr&gt;
     &lt;/tbody&gt;
   &lt;/table&gt;


Is it necessary to use so many foreach loops? Do you have any other better way of getting this output?

  $sql = "SELECT * FROM product WHERE name IN ('Product 1','Product 2') ";
  if ($wpdb-&gt;query($sql))
  {
    $rows = $wpdb-&gt;last_result;
    $similar_com = "";
    $similar_com .= "&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;";

    foreach($rows as $row)
    { 
       $similar_com .= "&lt;img style='width:100px;height:100px' src='".$row-&gt;image_url."' alt='".$row-&gt;model_no."'&gt;";
    }
    $similar_com .= "&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Width&lt;/td&gt;";
    foreach($rows as $row)
    { 
       $similar_com .= "&lt;td&gt;$row-&gt;width mm&lt;/td&gt;";
    }
    $similar_com .= "&lt;/tr&gt;&lt;td&gt;Material&lt;/td&gt;";
    foreach($rows as $row)
    { 
       $similar_com .= "&lt;td&gt;$row-&gt;material&lt;/td&gt;";
    }
    $similar_com .= "&lt;/tr&gt;&lt;td&gt;Case diameter&lt;/td&gt;";
    foreach($rows as $row)
    { 
       $similar_com .= "&lt;td&gt;$row-&gt;case_diameter (".plus($product-&gt;case_diameter - $row-&gt;case_diameter)." mm)&lt;/td&gt;";
    }
    $similar_com .= "&lt;/tr&gt;&lt;td&gt;Case thickness&lt;/td&gt;";

    foreach($rows as $row)
    { 
       $similar_com .= "&lt;td&gt;$row-&gt;case_thickness (".plus($product-&gt;case_thickness - $row-&gt;case_thickness)." mm)&lt;/td&gt;";
    }
    $similar_com .= "&lt;/tr&gt;&lt;td&gt;Case Material&lt;/td&gt;";
    foreach($rows as $row)
    { 
       $similar_com .= "&lt;td&gt;$row-&gt;case_material&lt;/td&gt;";
    }
    $similar_com .= "&lt;/tr&gt;&lt;td&gt;Battery Hours&lt;/td&gt;";
    foreach($rows as $row)
    { 
       $similar_com .= "&lt;td&gt;$row-&gt;battery_hours hours&lt;/td&gt;";
    }
    $similar_com .= "&lt;/tr&gt;&lt;td&gt;Special Functions&lt;/td&gt;";
    foreach($rows as $row)
    { 
       $similar_com .= "&lt;td&gt;$row-&gt;special_functions&lt;/td&gt;";
    }

    $similar_com .= "&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;";
   }
Is it critical that your product be the top row? It seems like you would benefit tremendously if your table was structured like this:

&lt;table&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;Product&lt;/th&gt;
            &lt;th&gt;Width&lt;/th&gt;
            &lt;th&gt;Material&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;th&gt;&lt;img /&gt;&lt;/th&gt;
            &lt;td&gt;50mm&lt;/td&gt;
            &lt;td&gt;Plastic&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;


because that way, you can just use a single foreach loop:

foreach ($rows as $row) {
    $similar_com .= "&lt;tr&gt;";
    $similar_com .= "&lt;td&gt;&lt;img style='width: 100px; height: 100px;' src='$row-&gt;image_url' alt='$row-&gt;model_no' /&gt;&lt;br /&gt;$row-&gt;name&lt;/td&gt;";
    $similar_com .= "&lt;td&gt;$row-&gt;width mm&lt;/td&gt;";
    ...
}

You're doing a SELECT *. Avoid that; it's considered better to explicitly state the columns you're using.
I would rather rename your $rows variable to $products, but I notice that you already have a $product variable. I don't know what this variable is, but you should consider renaming some things... $similar_com is an especially useless name; I can't tell what it's supposed to mean.
I noticed you're using WordPress. The $wpdb-&gt;query(); $wpdb-&gt;last_result pattern is not as good as simply doing:

if ($rows = $wpdb-&gt;get_results($sql) {

If you have to keep your table structure the same, you could have separate variables for each row, then do it all in one foreach:

$widthRow = "&lt;tr&gt;&lt;th&gt;Width&lt;/th&gt;";
$materialRow = "&lt;tr&gt;&lt;th&gt;Material&lt;/th&gt;";
...
foreach ($rows as $row) {
    $widthRow .= "&lt;td&gt;$row-&gt;width mm&lt;/td&gt;";
    $materialRow .= "&lt;td&gt;$row-&gt;material&lt;/td&gt;";
    ...
}
$widthRow .= "&lt;/tr&gt;";
$materialRow .= "&lt;/tr&gt;";