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

标签 javascript php html

我有 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/

相关文章:

javascript - 如何在 angularjs 中集成 jQuery 函数?

php - 获取基本路径/URL

php - 间隔一系列值,使它们不重叠

html 对象标签我们应该将类型指定为 video/mp4 来播放 mp4 视频吗?

javascript - 通过单击子区域中的按钮关闭模态内联对话框并防止其重新打开

javascript - React - 动态表中的 OnChange 调用会产生 Uncaught TypeError

javascript - 在 React 中创建图形

javascript - 为什么之后调用 JQuery slideToggle() 函数 tbody 显示 :block setted and its width appear compressed?

javascript - 尝试使用左、右、上箭头键来实现 15 款益智游戏的移动

php - 如何拆分日期时间戳记的时间部分?