3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Mock function to generate some random sales data, sometimes returning nothing. function getLocationSalesForDate(DateTime $date, string $location) { // Your business logic goes here. // Get the date as a string using DateTime::format(). // Return a random mock value for this demo. Will randomly return NULL so as to simulate // your table view. return (mt_rand(0, 3) === 1)?NULL:mt_rand(15, 70); } $locations = ['city 1', 'city 2', 'city 3', 'city 4', 'city 5', 'city 6']; $numColumns = 3; $dateStart = new DateTime; // 'P7D' means a period ('P') of 7 ('7') days ('D') from $dateStart. Consult the documentation of DateInterval's constructor for details. $dateEnd = (clone $dateStart)->add(new DateInterval('P7D')); // Segment your locations to however many you want to show on a single line. foreach (array_chunk($locations, $numColumns) as $columnHeadings) { // Output table haeding for this group of locations. $html = <<<EOT <table> <thead> <tr> <th>Date</th> EOT; // Write out each location as a column heading. foreach ($columnHeadings as $columnHeading) $html .= <<<EOT <th>$columnHeading</th> EOT; $html .= <<<EOT </tr> </thead> EOT; // Output sales per day for this location. $html .= <<<EOT <tbody> EOT; // Loop through each day between $dateStart and $dateEnd. $dateIterator = clone $dateStart; while ($dateIterator != $dateEnd) { // Start new row, print date on first column. $html .= <<<EOT <tr> <td>{$dateIterator->format('Y-m-d')}</td> EOT; // Loop through this segment's locations and fetch sales for this day. foreach ($columnHeadings as $location) { // Retrieve mock sales data for this day on this location. $sales = getLocationSalesForDate($dateIterator, $location); // Record sales if we have any. if ($sales) // Have sales. $html .= <<<EOT <td>$sales</td> EOT; else // No sales for this day. $html .= <<<EOT <td><!-- Empty cell if no sales recorded for this location on this day. --></td> EOT; } // Close sales data row. $html .= <<<EOT </tr> EOT; // Advance to next day for this location. $dateIterator->add(new DateInterval('P1D')); } // Close table for this location group. $html .= <<<EOT </tbody> </table> EOT; // Output table to user. echo $html; }

preferences:
29.33 ms | 405 KiB | 5 Q