HiveBrain v1.2.0
Get Started
← Back to all entries
snippetphpMinor

Foreach loop to generate HTML table with title in first cell of each row in PHP

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
celleachtablewithphploopgeneratefirstforeachrow

Problem

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:


     
        
         Product 1Product 2
        
     
     
        
          Width50mm90mm
          MaterialPlasticGold
          Diameter60 mm70mm
          Battery Hours60 hours50 hours
        
     
   


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->query($sql))
  {
    $rows = $wpdb->last_result;
    $similar_com = "";
    $similar_com .= "";

    foreach($rows as $row)
    { 
       $similar_com .= "image_url."' alt='".$row->model_no."'>";
    }
    $similar_com .= "Width";
    foreach($rows as $row)
    { 
       $similar_com .= "$row->width mm";
    }
    $similar_com .= "Material";
    foreach($rows as $row)
    { 
       $similar_com .= "$row->material";
    }
    $similar_com .= "Case diameter";
    foreach($rows as $row)
    { 
       $similar_com .= "$row->case_diameter (".plus($product->case_diameter - $row->case_diameter)." mm)";
    }
    $similar_com .= "Case thickness";

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

    $similar_com .= "";
   }

Solution

-
Is it critical that your product be the top row? It seems like you would benefit tremendously if your table was structured like this:


    
        
            Product
            Width
            Material
        
    
    
        
            
            50mm
            Plastic
        
    


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

foreach ($rows as $row) {
    $similar_com .= "";
    $similar_com .= "image_url' alt='$row->model_no' />$row->name";
    $similar_com .= "$row->width mm";
    ...
}


-
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->query(); $wpdb->last_result pattern is not as good as simply doing:

if ($rows = $wpdb->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 = "Width";
$materialRow = "Material";
...
foreach ($rows as $row) {
    $widthRow .= "$row->width mm";
    $materialRow .= "$row->material";
    ...
}
$widthRow .= "";
$materialRow .= "";

Code Snippets

<table>
    <thead>
        <tr>
            <th>Product</th>
            <th>Width</th>
            <th>Material</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th><img /></th>
            <td>50mm</td>
            <td>Plastic</td>
        </tr>
    </tbody>
</table>
foreach ($rows as $row) {
    $similar_com .= "<tr>";
    $similar_com .= "<td><img style='width: 100px; height: 100px;' src='$row->image_url' alt='$row->model_no' /><br />$row->name</td>";
    $similar_com .= "<td>$row->width mm</td>";
    ...
}
if ($rows = $wpdb->get_results($sql) {
$widthRow = "<tr><th>Width</th>";
$materialRow = "<tr><th>Material</th>";
...
foreach ($rows as $row) {
    $widthRow .= "<td>$row->width mm</td>";
    $materialRow .= "<td>$row->material</td>";
    ...
}
$widthRow .= "</tr>";
$materialRow .= "</tr>";

Context

StackExchange Code Review Q#58764, answer score: 7

Revisions (0)

No revisions yet.