PHP 如果列超过特定数量,则数组中断

PHP array break if the column exceeds specific number

你好,我尝试修改我在 2018 年聘用的开发人员留下的代码,但不是编码员确实对我造成了伤害。我想在这里做的是让这段代码创建另一个 table 如果第一个 table 循环有超过 3 个城市并重复创建 table 的过程而不是在中添加新列现有的 table、tables 默认情况下的 raows 等于一个月中的天数,但这些列作为位置从数据库中提取。这是代码。

if (!empty($locations)) {
    echo "<table class='table table-striped tbl tbl-result text-center top1'>\r\n\t<thead>\r\n\t\t<tr>\r\n\t\t\t<th>";
    echo lang("label_date");
    echo "</th>\r\n\t\t\t";
    foreach ($locations as $location) {
        echo "\t\t\t<th>";
        echo $location->name;
        echo "</th>\r\n\t\t\t";
    }
    echo "\t\t</tr>\r\n\t</thead>\r\n\t<tbody>\r\n\t";
    foreach ($dates as $date) {
        echo "\t\r\n\t<tr>\r\n\t\t<td>";
        echo $date;
        echo "</td>\r\n\t\t";
        foreach ($locations as $location) {
            echo "\t\t\t";
            $data = $this->sales_model->result_row($date, $location->id, $sales);
            echo "\t\t\t";
            if ($data) {
                echo "\t\t\t<td>";
                echo $this->sales_model->append_zero($data);
                echo "</td>\r\n\t\t\t";
            } else {
                echo "\t\t\t<td></td>\r\n\t\t\t";
            }
            echo "\t\t";
        }
        echo "\t</tr>\r\n\t";
    }
    echo "\t</tbody>\r\n</table>\r\n";
}

我尝试了 if else 语句并继续 break 但无济于事我什至不知道此时我在做什么。它显示的内容如下,我希望它在每个循环中打破 3 个城市 image

你的问题有点含糊。但是,如果我理解正确的话,您想将每个城市的销售数量限制为每个日期的前三列(city1、city2、city3)。同时保持整个城市 headers 完好无损。

foreach 可能不是执行此操作的最佳构造,但鉴于您目前的深度,我会建议一些接近您已有的东西。

尝试将第二次出现的 foreach ($locations as $location) { 替换为:

foreach ($locations as $locationIndex => $location) {
    if ($locationIndex > 2)
        break;

这假定 $locations 是一个具有连续数字索引的简单数组。如果这不是您想要做的,请详细说明,以便我提供更准确的答案。

更新

有关一种可能方法的现场演示,请参阅 this eval。我使用 heredoc 语法来构建 HTML,因为对于本示例而言,它更易于阅读。这是代码。您可以设置 $numColumns 来控制 table 列的数量。

<?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 heading 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;
}