php - 如何在 HTML 属性中使用 PHP 值

标签 php html mysql

我正在尝试使用数组来确定表中的颜色...但是,我相信我的语法不正确...

数组:

$colors = [
  1 => "red",
  2 => "yellow",
  3 => "green",
];

HTML/PHP:

<td bgcolor=\"$colors[$row['form1']]\">".$row["form1"]."</td>

感谢任何帮助,谢谢!

最佳答案

尝试以下方法

$doc= new DOMDocument(); $doc->loadHTML($html); $colors = array(1 => "red",2 => "yellow",3 => "green");foreach ($doc->getElementsByTagName('td') as $td) {if ($td->getAttribute('bgcolor'),$colors) { /* if true then any color in the array is equal to the td's bgcolor*/ } }

关于php - 如何在 HTML 属性中使用 PHP 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34691794/

相关文章:

PHP 函数调用 sql SUM 不起作用

javascript - 我可以在 <link> 标签 href 内调用 JavaScript 函数吗?

mysql - 在 Django 中实现 MySQL 年份类型的映射

mysql - 必须定义参数 '@myLeft'

php - 为什么会 print_r ($row);只返回数字 1?

php - 如何在 codeigniter 的 onclick 事件函数中将 json 编码的 php 变量作为参数传递

javascript - 传递给查看 PHP-JS 时,HTML 标记未正确呈现

html - 如何使用 css/html 在句子中间使用图标/图像

html - 创建一个带有 CSS 样式的盒子

mysql - 复杂的 SELECT 查询