我有 2 个文本文件,并使用 php 将其内容读入 2 个数组。然后我创建一个表格并将其显示在我的网站上。我现在想根据某些值对数组 2(第 2 列)中的每个单元格进行着色。
例如:
if a value from array2 contains "busy", it shall be formatted red, if it contains "available", it shall be formatted green.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form action="" method="post" class="formbody">
<div class="codetable">
<?php
$file1 = "c:/presencetool/ramfile1.txt";
$file2 = "c:/presencetool/ramfile2.txt";
$line1= file($file1, FILE_IGNORE_NEW_LINES);
$line2 = file($file2, FILE_IGNORE_NEW_LINES);
$combine = array_combine($line1, $line2);
$html = "<table align=center>";
$html .= "<tr><td width=300px></td><td></td></tr>";
$i = 1;
foreach ($combine as $file1 => $file2):
$html .= "<tr>";
$html .= "<td>".$file1."</td>";
$html .= "<td>".$file2."</td>";
$html .= "</tr>";
$i++;
endforeach;
$html .= "</table>";
echo $html;
?>
<div class="codelabel">
</div>
</div>
</form>
</body>
</html>
我尝试了一些可能的解决方案,但无法使其发挥作用。 有任何想法吗?如果我需要提供任何信息,请询问。
编辑: ramfile 1&2的内容:
ramfile1:
User@1
User@2
User@3
...
User@n
ramfile2:
Busy
Busy
Available
Busy
...
最佳答案
<?php
$path = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'demo'.DIRECTORY_SEPARATOR;
$file1 = $path.'ramfile1.txt';
$file2 = $path.'ramfile2.txt';
if(file_exists($file1) && file_exists($file2)) {
$line1= file($file1, FILE_IGNORE_NEW_LINES);
$line2 = file($file2, FILE_IGNORE_NEW_LINES);
$combine = array_combine($line1, $line2);
$html = '<table align="center">';
$html .= '<tr><td width="300px"></td><td></td></tr>';
$i = 1;
foreach ($combine as $key => $value):
$html .= '<tr class="'.$value.'">';
$html .= '<td>'.$key.'</td>';
$html .= '<td>'.$value.'</td>';
$html .= '</tr>';
$i++;
endforeach;
$html .= '</table>';
echo $html;
}
?>
<style>
.Busy { background-color: #f2a179;}
.Available { background-color: #00ff00;}
</style>
关于javascript - 根据php中的值格式化单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55472459/