Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果列超过特定数字,则PHP数组将中断_Php_Arrays_Foreach Loop Container - Fatal编程技术网

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

如果列超过特定数字,则PHP数组将中断,php,arrays,foreach-loop-container,Php,Arrays,Foreach Loop Container,你好,我试着修改我在2018年雇佣的开发人员留下的代码,但不是一名编码员真的让我付出了代价。我想在这里做的是让这段代码创建另一个表如果第一个表循环有3个以上的城市,并重复创建表的过程,而不是在现有表中添加新列,默认情况下,这些表的RAOW等于月份中的天数,但这些列是从数据库中作为位置显示的。这是代码 if (!empty($locations)) { echo "<table class='table table-striped tbl tbl-result text-c

你好,我试着修改我在2018年雇佣的开发人员留下的代码,但不是一名编码员真的让我付出了代价。我想在这里做的是让这段代码创建另一个表如果第一个表循环有3个以上的城市,并重复创建表的过程,而不是在现有表中添加新列,默认情况下,这些表的RAOW等于月份中的天数,但这些列是从数据库中作为位置显示的。这是代码

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";
}
如果(!空($locations)){
echo“\r\n\t\r\n\t\t\r\n\t\t\t”;
echo lang(“标签日期”);
回显“\r\n\t\t\t”;
foreach($locations作为$location){
回显“\t\t\t”;
echo$location->name;
回显“\r\n\t\t\t”;
}
回显“\t\t\r\n\t\r\n\t\r\n\t”;
foreach($日期作为$日期){
回显“\t\r\n\t\r\n\t\t”;
echo$日期;
回显“\r\n\t\t”;
foreach($locations作为$location){
回显“\t\t\t”;
$data=$this->sales\u model->result\u行($date,$location->id,$sales);
回显“\t\t\t”;
如果($数据){
回显“\t\t\t”;
echo$this->sales\u model->append\u zero($data);
回显“\r\n\t\t\t”;
}否则{
回显“\t\t\r\n\t\t\t”;
}
回显“\t\t”;
}
回显“\t\r\n\t”;
}
回显“\t\r\n\r\n”;
}

我尝试了if-else语句并继续中断,但没有用,我甚至不知道我现在在做什么。它显示的内容如下,我希望它在每个循环中打断3个城市

您的问题有点模糊。但是如果我理解正确的话,您希望将每个城市的销售额限制为每个日期的前三列(城市1、城市2、城市3)。同时让所有的城市头目保持机智

foreach
可能不是实现这一点的最佳方法,但鉴于您的深度已经超出了实际情况,我将建议您使用与您已有的接近的方法

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

foreach ($locations as $locationIndex => $location) {
    if ($locationIndex > 2)
        break;
这假设
$locations
是一个带有顺序数字索引的简单数组。如果这不完全是您想要做的,请详细说明,以便我可以提供更准确的答案

更新

有关一种可能的方法的实时演示,请参阅。我使用了heredoc语法来构建HTML,因为在本示例中它更易于阅读。下面是代码。您可以设置
$numColumns
来控制表列的数量

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

请精确-给我们一个你想要的精确布局的例子。你想为每组3个城市单独设置一个表,还是什么?你是想限制城市的数量,还是日期的数量?@ADyson是的,我想为每组3个城市单独设置表,这样就不会超出页面大小。页面具体是什么?网页没有固定的页面大小。你是说什么时候打印?是的,打印的时候,还要禁用水平滚动。谢谢你的帮助。我想做的是创建另一个表,如果一个表中有3个以上的城市,代码当前不断地在单个表中添加城市,并且它们超出了标准页面大小正在体验用户体验。仍然需要一些澄清。我的理解是否正确,您希望保留列/行布局,但您希望在第一个表的“City 3”之后停止,并在其下方输出一个全新的表(该表再次使用您显示的布局)?按页面大小,您指的是将要打印的内容?或者您是否试图阻止水平滚动?是的,如果当前表格超过3个城市,请创建新表格,按页面大小,我希望它阻止水平滚动,有时甚至打印。@AnkitY.,请查看我的更新答案。