Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.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将数组数据映射到HTML表,同时跳过一些HTML表列_Php_Html - Fatal编程技术网

PHP将数组数据映射到HTML表,同时跳过一些HTML表列

PHP将数组数据映射到HTML表,同时跳过一些HTML表列,php,html,Php,Html,阿洛 我有一个php数组: $data=array(1,2,3,4,5,6,7,8); 我想把它放在具有以下结构(图像)的html表中 将完全跳过彩色列。我未能做到这一点,但以下是我的尝试: $totalColumnsPerRow=8; $skippableColumns=array(3,6,7,8); $counter=1; //loop the array now $row="<tr>"; foreach($data as $val){ //do we need to sta

阿洛

我有一个php数组:

$data=array(1,2,3,4,5,6,7,8);
我想把它放在具有以下结构(图像)的html表中

将完全跳过彩色列。我未能做到这一点,但以下是我的尝试:

$totalColumnsPerRow=8;
$skippableColumns=array(3,6,7,8);
$counter=1;
//loop the array now
$row="<tr>";
foreach($data as $val){
//do we need to start a new row or not?
 if ($counter==$totalColumnsPerRow){
 //close open row and create a new one.
 $counter=1;
 $row.="</tr><tr>":
  }

 //show I skip the current column or not?
 if(in_array($counter,$skippableColumns)){
 //skip column then add current value
 $row.="<td></td>";
 $row.="<td>$val</td>";
 }
 else{
 $row.="<td>$val</td>";
  }
 $counter++;
}
$totalColumnsPerRow=8;
$skippableColumns=数组(3,6,7,8);
$counter=1;
//现在循环数组
$row=“”;
foreach($val形式的数据){
//我们是否需要重新开始?
如果($counter==$totalColumnsPerRow){
//关闭打开的行并创建一个新行。
$counter=1;
$row.=“”:
}
//是否显示跳过当前列?
if(在数组中($counter,$skippableColumns)){
//跳过列,然后添加当前值
$row.=”;
$row.=“$val”;
}
否则{
$row.=“$val”;
}
$counter++;
}
这给了我下表的结构。如果已成功跳过这些行,则新行将以值5开头

这是一个


你知道如何暂停循环并在可用列中使用它吗?我的方法实用吗?

我修改了你的代码。请尝试以下方法。添加注释以更好地理解

<table border="1" width="600px">
<tr>
<td>A</td><td>B</td><td>ABT</td><td>C</td><td>D</td><td>CDT</td><td>ACT</td><td>TTT</td>
</tr>

<?php

$data = array(1,2,3,4,5,6,7,8);
$totalColumnsPerRow = 8;
$skippableColumns = array(3,6,7,8);

$table = '<tr>';
$lastIndex = 0;

for($i = 1; $i <= $totalColumnsPerRow; $i++) { // Per column

    if(in_array($i, $skippableColumns)) { // Skipping coulmn value
        $table .= '<td></td>';
    } else { // Adding coulmn value
        $table .= '<td>'.$data[$lastIndex].'</td>';
        $lastIndex++; // Incrementing data index
    }

    if($i == $totalColumnsPerRow) { // Last column
        $table .= '</tr>'; // Ending row

        if($lastIndex <= count($data) -1) { // Data left
            $table .= '<tr>'; // Starting new row

            $i = 0; // Resetting so in the next increment it will become 1
        }
    }
}

echo $table;
?>

ababtcddactttt
输出为

我修改了您的代码,在同一个循环中(使用相同的$val数据)使用很少使用的、被恶意攻击的控件进行重新检查。对现有代码的最小更改

foreach($data as $val){
    //we'll come back here to decide on skip or write of next row when a skippable is encountered
    //this keeps us locked onto the same $val until it is written out
    recheck: 
    //do we need to start a new row or not?
    if ($counter==$totalColumnsPerRow){
    //close open row and create a new one.
        $counter=1;
        $row.="</tr><tr>";
    }

    //show I skip the current column or not?
    if(in_array($counter,$skippableColumns)){
    //skip column then add current value
        $row.="<td></td>";
        $counter++;
        goto recheck;
    //   $row.="<td>$val</td>";
    }
    else{
        $row.="<td>$val</td>";
    }
    $counter++;
}
foreach($val数据){
//我们将回到这里来决定在遇到可跳过项时跳过或写入下一行
//这会使我们一直锁定在相同的$val上,直到它被写出来
复查:
//我们是否需要重新开始?
如果($counter==$totalColumnsPerRow){
//关闭打开的行并创建一个新行。
$counter=1;
$row.=”;
}
//是否显示跳过当前列?
if(在数组中($counter,$skippableColumns)){
//跳过列,然后添加当前值
$row.=”;
$counter++;
去复查;
//$row.=“$val”;
}
否则{
$row.=“$val”;
}
$counter++;
}

尝试为每一行添加单独的循环。看起来这将是一个非常缓慢的过程,但让我给它一个机会。效果很好。让我看看其他建议并比较。谢谢你,伙计。“也很清楚,很有效率。”尼赛尔很高兴我能帮助你