从数组中插入数据到html文件



我有一个csv文件,我必须打开并转换为数组。然后我必须打开html文件,插入这个数组到特定的地方,然后保存这个文件。这很重要,我必须有两个文件。我要给你看我的代码。create.php

session_start();
$file = 'people.csv';
$html = 'sign.php';
if(file_exists($file)) //here I convert csv file to array
{
$handle = fopen($file, "r");
while (($line = fgetcsv($handle, 0, ";")) !== FALSE) {
$array_line_full[] = $line;
}
fclose($handle);
foreach ($array_line_full as $array) // here i try to send this data vie session to this file, but it doesn't work
{
$_SESSION['array'] = $array;
$_SESSION['name'] = $array[0];
$_SESSION['spec'] = $array[1];
$_SESSION['email'] = $array[2];
$_SESSION['num'] = $array[3];
//            include('sign.php');
//            $f = fopen($html, 'w');
//            fwrite($f, $_SESSION['array']);
//            fclose($f);
//            $res = file_get_contents('sign.php');
$res = file_put_contents($html, $_SESSION['array']);
$newFile = fopen($array[0].'_HTML.html', 'w');
fwrite($newFile, $res);
fclose($newFile);
echo "fgbfbgn";
}

这是我的html文件。重要的是,该文件必须尽可能保持清晰(我的意思是没有php代码)

<?php
$array = $_SESSION['array'];
?>
<body>
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" style="color:#3b9ed5">
<div><?= $array[0] ?></div>
<div><?= $array[1] ?></div>
<div> <?= $array[2] ?></div>
</td>
</tr>
...

我能做些什么来添加信息从php文件到html文件在这些确切的地方,然后保存文件与此插入数据不使用JS(如果可能的话)

注:我不是以英语为母语的人,所以我为我的错误感到抱歉

如果我做对了,这里有一个例子:

create.php

//... prepare csv to array
//create html 
$output ='';
foreach($_SESSION['array'] as $data){
$output .= '<div>'.$data.'</div>';
}
ob_start();
include 'html.php';
$cached = ob_end_clean();
file_put_contents('generated.html',$cached);

html.php

<body>
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" style="color:#3b9ed5">
<?= $output ?>
</td>
</tr>
</table>
</div>
</body>

ob_start();&ob_end_clean()缓存包含html.php.

时生成的输出。然后我们可以将缓存的输出写入一个文件。

最新更新