I have a variable (array) and is returning the following:
array(13) {
["meta_id"]=> string(1) "2"
["post_id"]=> string(4) "2112"
["provider"]=> string(12) "Abc news"
["scheme"]=> string(3) "morning"
["viewers"]=> string(9) "2000"
["min_time"]=> string(3) "5"
["max_time"]=> string(3) "100"
["bways"]=> string(2) "10"
["release_date"]=> string(10) "04-05-2021"
["type"]=> string(11) "Video"
["variance"]=> string(4) "4k"
["tech"]=> string(9) "JS, HTML5"
["game_id"]=> string(5) "17640"
}
I get this data from the following query:
$meta = $wpdb->get_row( $wpdb->prepare("
SELECT * FROM ".$table_name." WHERE post_id = %d
", $post_id), ARRAY_A);
Currently, I am outputting the data in a table using something like:
$output .="<tr>";
$output .= sprintf('<td>%s</td><td>%s</td>', $attribute, $value);
$output .="</tr>";
and it is generating a table with 2 columns. I want to have a 4 columns instead like in the screenshot below:
Left column with a maximum of 5 rows and the remaining rows on the other column.
Based on this data array:
$meta = [
"meta_id" => "2",
"post_id" => "2112",
"provider" => "Abc news",
"scheme" => "morning",
"viewers" => "2000",
"min_time" => "5",
"max_time" => "100",
"bways" => "10",
"release_date" => "04-05-2021",
"type" => "Video",
"variance" => "4k",
"tech" => "JS, HTML5",
"game_id" => "17640",
];
You can use the "Modulo" operator to get the data dispatched in a table with 4 columns like:
$index = 0; // Initializing
$count = count($meta);
$output = '<table>
<tr><th>Attribute</th><th>Value</th><th>Attribute</th><th>Value</th></tr>
<tr>';
// Loop through array keys / values
foreach ( $meta as $attribute => $value ) {
$output .= sprintf('<td>%s</td><td>%s</td>', $attribute, $value);
$output .= $index % 2 === 0 && ($count - 1) === $index ? '<td></td><td></td>' : '';
$output .= $index % 2 === 1 ? '</tr><tr>' : '';
$index++;
}
echo $output . '</tr>
</table>';
It should work as expected. you will get somethiing like: