我已经将数组数据转换为.html文件,但现在想使用php附加html数据


i have converted array data to .html file but now want to append html data using php

我想将数组数据转换为HTML格式,并将文件保存为.HTML文件。我该怎么做?

假设数组为:

Array
(
[0-50] => 880
[50-100] => 5
[100-150] => 1
[550-600] => 1
)

我正在写一个这样的文件:

<?php
$file = fopen("abc.html","w");
?>

我可以很容易地将这个文件写为CSV格式,但如何从数据中存储html文件?

我的预期结果是-

长途旅行0-50 88050-100 5100-150 1550-600 1

这里的distance和trips是表头,distance字段中的all是数组的键,trips中的all则是数组的值。

假设我已经将数据写入这个文件->

$html['head'] = '<html><head></head><body>';
$html['content'] = "<table><tr style='background-color:cyan'><td>Distance</td><td>total Trips</td></tr>";
foreach($de as $key=>$value)
 {
$html['content'] .="<tr style='background-color:yellow'><td>$key</td><td>$value</td></tr>";
 }
 $html['foot'] = '</table></body>';
file_put_contents("abc.html", $html);

现在我想用这个数据附加这个文件->

$html['head'] = '<html><head></head><body>';
$html['content'] = "<h2>Data Regarding 0 - 50 km distance</h2>
<table width=900 border=5>
<tr style='background-color:cyan font-size:24px'>
<td>S.No</td>
<td>Customer Id</td>
<td>Total Distance</td>
<td>Total Trips</td>
<td>Average Speed</td>
";
foreach($dec as $result=>$value) {
$html['content'] .= "<tr style='background-color:lightgreen'><td>$value[cid]</td><td>$value[totalDistance]</td><td>$value[totalTrips]</td><td>$value[avgSpeed]</td></tr>";
}    
$html['foot'] = '</table></body></html>';

由于您将动态信息放置在静态HTML文档中,因此每次编辑时都需要重写这些信息。

  1. 使用一些PHP库读取HTML文件(如HTML DOM Parser)
  2. 从HTML文件中收集所有表信息
  3. 附加新信息(或在需要的地方插入)
  4. 将新内容写入HTML文件

我建议将这些信息保存为某种结构化格式(如json文件或数据库),在显示信息时,只需读取这些数据。这将更容易实施。

例如重写json文件中的数据:

$json = json_decode(file_get_contents('data.json'), true);
$json = array_merge($json, $newData);
file_put_contents(json_encode($json));

对于要放在html文件中的第一个表,这将使用您的方法来完成工作:

<?php 
//Creating the array
$de = Array("0-50" => 880,
"50-100" => 5,
"100-150" => 1,
"550-600" => 1
);
//creating the array corresponding to html code
$html = array();
//Creating the header
$html['head'] = '<html><head></head><body>';
//creating content - table header
$html['content'] = "<table><tr style='background-color:cyan'><td>Distance</td><td>total Trips</td></tr>";
foreach($de as $key=>$value)
{
    $html['content'] .="<tr style='background-color:yellow'><td>$key</td><td>$value</td></tr>";
}
//footer
$html['foot'] = '</table></body>';
//no need to use fopen because file_put_contents manages that.
file_put_contents("abc.html", $html);
?>

但对于问题的第二部分,同样的逻辑也会起作用,只是要小心如何使用数组,特别是在这里:

foreach($dec as $result=>$value)
{
    $html['content'] .= "<tr style='background-color:lightgreen'><td>$value[cid]</td><td>$value[totalDistance]</td><td>$value[totalTrips]</td><td>$value[avgSpeed]</td></tr>";
}

你应该这样写:

foreach($dec as $result=>$value) 
{
    $html['content'] .= "<tr style='background-color:lightgreen'><td>".$value['cid']."</td><td>".$value['totalDistance']."</td><td>".$value['totalTrips']."</td><td>".$value['avgSpeed']."</td></tr>";
}

嗨,希望它有帮助。

您想要的是附加模式。

a => Open for writing only;

a+ => Open for reading and writing;

这种模式的作用是"将文件指针放在文件的末尾。如果文件不存在,请尝试创建它。"

所以你的fopen行变成了。。。

$file = fopen("abc.html","a");

这样,你就可以随时附加你想要的任何内容。