以下代码出了什么问题?
$cores = array ("#FF0000","#FFBF00","#FFFF00","#04B404","#58FAF4","#0101DF");
foreach ($cores as $cor)
{
echo "<tr>";
echo "<td bgcolor = $cor></td>";
echo "</tr>";
}
但是这段代码有效:
$cores = array ("#FF0000","#FFBF00","#FFFF00","#04B404","#58FAF4","#0101DF");
for ($i=0; $i<7; $i++)
{
echo "<tr>";
echo "<td bgcolor=$cores[$i]></td>";
echo "</tr>";
}
此外,它不是在列中给出颜色(这是目标),而是在行中。
答案 0 :(得分:2)
您在变量周围缺少引号:
foreach ($cores as $cor)
{
echo "<tr>";
echo "<td bgcolor = '$cor'></td>";
echo "</tr>";
}
答案 1 :(得分:1)
这样做:
$colors = array("#FF0000","#FFBF00","#FFFF00","#04B404","#58FAF4","#0101DF");
$color = $colors[array_rand($colors)];
并将$color
变量传递给td
echo "<tr>";
echo "<td bgcolor = '$color'></td>";
echo "</tr>";
它将随机拾取颜色。
答案 2 :(得分:0)
如果您的目标是列,请使用:
<?php
$cores = array ("#FF0000","#FFBF00","#FFFF00","#04B404","#58FAF4","#0101DF");
echo "<table>";
echo "<tr>";
foreach ($cores as $cor)
{
echo "<td bgcolor= \"$cor\">1</td>";
}
echo "</tr>";
echo "</table>";
?>