gpt4 book ai didi

javascript - 根据php中的值格式化单元格

转载 作者:行者123 更新时间:2023-12-02 23:54:20 26 4
gpt4 key购买 nike

我有 2 个文本文件,并使用 php 将其内容读入 2 个数组。然后我创建一个表格并将其显示在我的网站上。我现在想根据某些值对数组 2(第 2 列)中的每个单元格进行着色。

例如:

if a value from array2 contains "busy", it shall be formatted red, ifit 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com