Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.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对CSV文件进行排序并显示在HTML表格中_Php_Html_Csv_Html Table - Fatal编程技术网

使用PHP对CSV文件进行排序并显示在HTML表格中

使用PHP对CSV文件进行排序并显示在HTML表格中,php,html,csv,html-table,Php,Html,Csv,Html Table,我有一个CSV文件,其中包含以下(虚拟)数据: Mike Judge,办公室,喜剧 拉里·沃乔夫斯基,矩阵零,行动 索菲亚·科波拉,《迷失在翻译中》,戏剧 罗恩·霍华德,美丽的心灵,戏剧 乔·拜登,你的大帽子,运动 巴拉克·奥巴马,固执的山羊,戏剧 史蒂夫·史密斯,两只美丽的蚂蚁,悬念 杰瑞德·赫斯,《拿破仑炸药》,喜剧 杰克·丹尼尔斯,《梦想的领域》,戏剧 我需要按姓氏字母顺序排列的第一个字段。这很有效 然后我需要在HTML表中显示数组,并能够将列数设置为5。我需要每个单元格保留CSV文件中

我有一个CSV文件,其中包含以下(虚拟)数据:

Mike Judge,办公室,喜剧
拉里·沃乔夫斯基,矩阵零,行动
索菲亚·科波拉,《迷失在翻译中》,戏剧
罗恩·霍华德,美丽的心灵,戏剧
乔·拜登,你的大帽子,运动
巴拉克·奥巴马,固执的山羊,戏剧
史蒂夫·史密斯,两只美丽的蚂蚁,悬念
杰瑞德·赫斯,《拿破仑炸药》,喜剧
杰克·丹尼尔斯,《梦想的领域》,戏剧
我需要按姓氏字母顺序排列的第一个字段。这很有效

然后我需要在HTML表中显示数组,并能够将列数设置为5。我需要每个单元格保留CSV文件中的一行,然后从左向右填充表格

我尝试过的每一件事都让我几乎达到了目的,但该表没有正确显示-它缺少td标记或在上一个td中没有

任何帮助都将不胜感激

以下是我目前掌握的情况:

 $file = fopen("dummy-list.csv", "r") or die('cannot open file');

// sort
$surnames = array();
$rows = array();
//build array of surnames, and array of rows
while (false != ( $row = fgetcsv($file, 0, ',') )) {
    //extract surname from the first column
    //this should match the last word before the comma, if there is a comma
    preg_match('~([^\s]+)(?:,.*)?$~', $row[0], $m);
    $surnames[] = $m[1];
    $rows[] = $row;
}
//fclose($file);
//sort array of rows by surname using our array of surnames
array_multisort($surnames, $rows);
//print_r($rows);


// set # of table columns
$numCols = 4;
$i=0;


echo '<table border="1" align="center"><tr>' . "\n";
foreach( $rows as $rows2 )
{

if ($i % $numCols == 0 && $i != 0) 
    echo "</tr>\n<tr>"; // every time but the first

    echo '<td>';

    foreach( $rows2 as $key )
    {
            echo $key . '<br />';
        $i++;
    }
    echo "</td>\n";
}


while ($i++ % $numCols != 0) 
echo '<td>&nbsp;</td>' . "\n"; // fill in empty cells


// end table
echo '</tr></table>' . "\n\n\n";
$file=fopen(“dummy list.csv”、“r”)或die(“无法打开文件”);
//分类
$namess=array();
$rows=array();
//构建姓氏数组和行数组
而(false!=($row=fgetcsv($file,0,,')){
//从第一列中提取姓氏
//如果有逗号,则应与逗号前的最后一个单词匹配
preg_match(“~([^\s]+)(?:,.*)$~”,$row[0],$m);
$姓氏[]=$m[1];
$rows[]=$row;
}
//fclose($文件);
//使用我们的姓氏数组按姓氏对行数组进行排序
数组_multisort($姓氏,$行);
//打印(行);
//表列集合
$numCols=4;
$i=0;
回显“”。“\n”;
foreach($rows作为$rows2的行)
{
如果($i%$numCols==0&&$i!=0)
echo“\n”//除了第一次
回声';
foreach($rows2作为$key)
{
回显$key.“
”; $i++; } 回音“\n”; } 而($i++%$numCols!=0) 回显“”。“\n”;//填空 //端台 回显“”。“\n\n\n”;
您可以使用MOD运算符检查您是否在列的末尾,然后结束行,并开始新的行:

$cols = 4;
$count = count($rows);

$table_html ="<tr>";
for($i=0; $i < $count; ++$i){
    $table_html.= '<td>' . implode("<br>", $rows[$i]) . '</td>';
    if($i != 0 && ($i % $cols == 0)){
        $table_html.="</tr>";
    }
}
$table_html.="</tr>";
echo $table_html;
$cols=4;
$count=计数($rows);
$table_html=“”;
对于($i=0;$i<$count;++$i){
$table_html.=''.introde(
“,$rows[$i])”; 如果($i!=0&($i%$cols==0)){ $table_html.=“”; } } $table_html.=“”; echo$table_html;
您可以使用MOD运算符检查您是否在列的末尾,然后结束行,并开始新的行:

$cols = 4;
$count = count($rows);

$table_html ="<tr>";
for($i=0; $i < $count; ++$i){
    $table_html.= '<td>' . implode("<br>", $rows[$i]) . '</td>';
    if($i != 0 && ($i % $cols == 0)){
        $table_html.="</tr>";
    }
}
$table_html.="</tr>";
echo $table_html;
$cols=4;
$count=计数($rows);
$table_html=“”;
对于($i=0;$i<$count;++$i){
$table_html.=''.introde(
“,$rows[$i])”; 如果($i!=0&($i%$cols==0)){ $table_html.=“”; } } $table_html.=“”; echo$table_html;
您只需稍微修改一下逻辑即可。目前,您的脚本无法跟踪您的列,因此无法知道要填充多少单元格

最大的逻辑问题是:

while ($i++ % $numCols != 0) 
echo '<td>&nbsp;</td>' . "\n"; // fill in empty cells
while($i++%$numCols!=0)
回显“”。“\n”;//填空
所做的只是检查列的数量是否可以由$i设计。所以,在$i达到3后,它停止。(在这种情况下,4/3!=0)

以下工作:

$file = fopen("dummy-list.csv", "r") or die('cannot open file');

// sort
$surnames = array();
$rows = array();
//build array of surnames, and array of rows
while (false != ( $row = fgetcsv($file, 0, ',') )) {
   //extract surname from the first column
   //this should match the last word before the comma, if there is a comma
   preg_match('~([^\s]+)(?:,.*)?$~', $row[0], $m);
   $surnames[] = $m[1];
   $rows[] = $row;
}
//fclose($file);
//sort array of rows by surname using our array of surnames
array_multisort($surnames, $rows);
//print_r($rows);


// set # of table columns
$numCols = 4;
$i=0;
$cellpos = 0; //count columns

echo '<table border="1" align="center"><tr>' . "\n";
//use the amount of rows as a divisor
for($c=0; $c<Count($rows); $c++ )
{

    if ($i % $numCols == 0 && $i != 0){
       echo "</tr>\n<tr>"; // every time but the first
       $cellpos=0;  //reset our column position
    }

    echo '<td>';
    //then, call each csv row explicitly
    foreach( $rows[$c] as $key )
    {
        echo $key . '<br />';
        $i++;
    }
    echo "</td>\n";
    $cellpos++;  //track our column position
}

while ($cellpos++ < $numCols)
echo '<td>&nbsp;</td>' . "\n"; // fill in empty cells


// end table
echo '</tr></table>' . "\n\n\n";
$file=fopen(“dummy list.csv”、“r”)或die(“无法打开文件”);
//分类
$namess=array();
$rows=array();
//构建姓氏数组和行数组
而(false!=($row=fgetcsv($file,0,,')){
//从第一列中提取姓氏
//如果有逗号,则应与逗号前的最后一个单词匹配
preg_match(“~([^\s]+)(?:,.*)$~”,$row[0],$m);
$姓氏[]=$m[1];
$rows[]=$row;
}
//fclose($文件);
//使用我们的姓氏数组按姓氏对行数组进行排序
数组_multisort($姓氏,$行);
//打印(行);
//表列集合
$numCols=4;
$i=0;
$cellpos=0//数列
回显“”。“\n”;
//使用行数作为除数

对于($c=0;$c),您只需稍微修改一下逻辑即可。目前,您的脚本无法跟踪要填充的列,因此无法知道要填充多少个单元格

最大的逻辑问题是:

while ($i++ % $numCols != 0) 
echo '<td>&nbsp;</td>' . "\n"; // fill in empty cells
while($i++%$numCols!=0)
回显“”。“\n”//填写空单元格
所做的只是检查列的数量是否可以由$i设计。因此,在$i达到3后,它停止。(在本例中,4/3!=0)

以下工作:

$file = fopen("dummy-list.csv", "r") or die('cannot open file');

// sort
$surnames = array();
$rows = array();
//build array of surnames, and array of rows
while (false != ( $row = fgetcsv($file, 0, ',') )) {
   //extract surname from the first column
   //this should match the last word before the comma, if there is a comma
   preg_match('~([^\s]+)(?:,.*)?$~', $row[0], $m);
   $surnames[] = $m[1];
   $rows[] = $row;
}
//fclose($file);
//sort array of rows by surname using our array of surnames
array_multisort($surnames, $rows);
//print_r($rows);


// set # of table columns
$numCols = 4;
$i=0;
$cellpos = 0; //count columns

echo '<table border="1" align="center"><tr>' . "\n";
//use the amount of rows as a divisor
for($c=0; $c<Count($rows); $c++ )
{

    if ($i % $numCols == 0 && $i != 0){
       echo "</tr>\n<tr>"; // every time but the first
       $cellpos=0;  //reset our column position
    }

    echo '<td>';
    //then, call each csv row explicitly
    foreach( $rows[$c] as $key )
    {
        echo $key . '<br />';
        $i++;
    }
    echo "</td>\n";
    $cellpos++;  //track our column position
}

while ($cellpos++ < $numCols)
echo '<td>&nbsp;</td>' . "\n"; // fill in empty cells


// end table
echo '</tr></table>' . "\n\n\n";
$file=fopen(“dummy list.csv”、“r”)或die(“无法打开文件”);
//分类
$namess=array();
$rows=array();
//构建姓氏数组和行数组
而(false!=($row=fgetcsv($file,0,,')){
//从第一列中提取姓氏
//如果有逗号,则应与逗号前的最后一个单词匹配
preg_match(“~([^\s]+)(?:,.*)$~”,$row[0],$m);
$姓氏[]=$m[1];
$rows[]=$row;
}
//fclose($文件);
//使用我们的姓氏数组按姓氏对行数组进行排序
数组_multisort($姓氏,$行);
//打印(行);
//表列集合
$numCols=4;
$i=0;
$cellpos=0;//计算列数
回显“”。“\n”;
//使用行数作为除数
对于($c=0;$c请继续使用您的代码:

更改此项:

while ($i++ % $numCols != 0) 
echo '<td>&nbsp;</td>' . "\n"; // fill in empty cells
while($i++%$numCols!=0)
回显“”。“\n”//填写空单元格
对于it:

$mod = $numCols-count($rows)%$numCols;

for($i=0;($i < ($mod) && $mod != $numCols);$i++)
    echo '<td>&nbsp;</td>' . "\n";
$mod=$numCols count($rows)%$numCols;
对于($i=0;($i<($mod)&&&$mod!=$numCols);$i++)
回显“”。“\n”;
并解决您的问题!

继续使用您的代码:

更改此项:

while ($i++ % $numCols != 0) 
echo '<td>&nbsp;</td>' . "\n"; // fill in empty cells
while($i++%$numCols!=0)
回显“”。“\n”//填写空单元格
对于it:

$mod = $numCols-count($rows)%$numCols;

for($i=0;($i < ($mod) && $mod != $numCols);$i++)
    echo '<td>&nbsp;</td>' . "\n";
$mod=$numCols count($rows)%$numCols;
对于($i=0;($i<($mod)&&&$mod!=$numCols);$i++)
回显“”。“\n”;

并解决您的问题!

我通过将CSV转换为JSON并使用DataTables来实现这一点这就是我们在1998年使用tables的目的。您有没有一个例子?GolezTrol-您的评论没有帮助,除非您打算在2016年推荐您现在使用的内容…我通过将CSV转换为JSON并使用DataTables来实现这一点这就是我们在1998年使用表格的原因。你有例子吗?GolezTrol-你的