如何解决如果列超过特定数量,则PHP数组中断
你好,我试图修改我2018年聘用的开发人员遗留下来的代码,但不是编码员,这确实给我造成了损失。我要在这里做的是,如果第一个表循环中有3个以上的城市,请使用此代码创建另一个表,然后重复此过程以创建表,而不是在现有表中添加新列,默认情况下,表的行数等于天以月为单位,但从数据库中将列作为位置。这是代码。
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语句并继续中断,但无济于事,我什至不知道我现在在做什么。它显示在下面,我希望它在每个循环image
中打破3个城市解决方法
您的问题有点含糊。但是,如果我对您的理解正确,则希望将每个日期的每个城市的销售数量限制为前三列(city1,city2,city3)。同时保持所有城市标题不变。
foreach
可能不是执行此操作的最佳方法,但是鉴于您的深度,我将建议一些与您已经拥有的内容接近的东西。
尝试将foreach ($locations as $location) {
的第二次出现替换为:
foreach ($locations as $locationIndex => $location) {
if ($locationIndex > 2)
break;
这假设$locations
是具有顺序数字索引的简单数组。如果这不是您要执行的操作,请详细说明,以便我提供更准确的答案。
更新
有关一种可能方法的实时演示,请参见this eval。我使用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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。