I have several fields from a mysql table (width,diameter etc). Instead of building up the strings from returned data one by one
$rows = $wpdb->last_result; // Wordpress version
foreach(array_chunk($rows, 5) as $i => $pair)
{
$width .= "<tr><td>Width</td>";
$diameter .= "<tr><td>Diameter</td>";
foreach($pair as $row)
{
$width .= "<td>$row->width mm</td>";
$diameter .= "<td>$row->diameter mm</td>";
}
$width .= "</tr>";
$diameter .= "</tr>";
}
Can I turn thickness,diameter,width into an array and loop over them like this:
foreach(array_chunk($rows, 5) as $i => $pair)
{
$measures = array("width"=>"Width","diameter"=>"Diameter","thickness"=>"Thickness","hours"=>"Hours");
foreach($measures as $measure=>$title)
{
$measure .= "<td>$title</td>";
}
foreach($pair as $row)
{
foreach($measures as $measure=>$title)
{
$measure .= "<td>$row->".$measure." mm </td>";
}
}
foreach($measures as $measure=>$title)
{
$measure .= "</tr>";
}
}
But I'm getting Object of class stdClass could not be converted to string error, so is it possible to do that?