[英]How do I put 5 text colors in 5 rows using for loop in multiplication table php?
thank you in advance for helping me.预先感谢您帮助我。 I am stuck with this problem.
我被这个问题困住了。 I should put different colors of text per row but I only know alternating 2 colors per row.
我应该每行放置不同颜色的文本,但我只知道每行交替使用 2 种颜色。 This should be the output
这应该是输出
<?php
$color1 = "#32CD32";
$color2 = "#FF0000";
$color3 = "#5e0087";
$color4 = "#FFA500";
$color5 = "#00008b";
$color = NULL;
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 5; $row++) {
echo "<tr> \n";
$color == $color1 ? $color = $color2: $color = $color1;
for ($col=1; $col <= 4; $col++) {
$num = $col * $row;
echo "<td style = 'color:$color'> $num </td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
You can create dynamic variables in php by creating the string (like "color1" and then putting $$
before it.您可以通过创建字符串(如“color1”,然后在其前面放置
$$
在 php 中创建动态变量。
$color1 = "#32CD32";
$color2 = "#FF0000";
$color3 = "#5e0087";
$color4 = "#FFA500";
$color5 = "#00008b";
$color = NULL;
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 5; $row++) {
echo "<tr> \n";
$color = "color".$row;
$color = $$color;
for ($col=1; $col <= 4; $col++) {
$num = $col * $row;
echo "<td style = 'color:$color'> $num </td> \n";
}
echo "</tr>";
}
Here's another option, put the colors in an array这是另一种选择,将颜色放入数组中
$colors = ["#32CD32", "#FF0000", "#5e0087", "#FFA500", "#00008b"];
then just access the array with the $row #然后只需使用 $row # 访问数组
$color = $colors[$row -1];
You should avoid using individual variables to store related data.您应该避免使用单个变量来存储相关数据。 By assigning these colors as elements in an array, you afford yourself the ability to loop over the collection of values without the use of "variable variables" (when your script seems to need variable variables, then it is time to rethink your script).
通过将这些颜色分配为数组中的元素,您可以在不使用“变量变量”的情况下循环遍历值的集合(当您的脚本似乎需要可变变量时,是时候重新考虑您的脚本了)。
$colors = [ 1 => "#32CD32", 2 => "#FF0000", 3 => "#5E0087", 4 => "#FFA500", 5 => "#00008B" ];
You should avoid writing inline styles as much as possible.您应该尽可能避免编写内联样式。 In truth, I'd recommend that you completely color your rows of text using pure css.
事实上,我建议您使用纯 css 为文本行完全着色。 However, I get the impression that this is just an exercise in writing dynamic code.
但是,我的印象是这只是编写动态代码的练习。
<style> table { border-collapse: collapse; width: 1%; } td { border: solid 1px black; padding: 8px; } </style>
I find myself paying an increasing amount of attention to generating clean well-tabbed source code (in addition to producing clean rendered html).我发现自己越来越关注生成干净的标签良好的源代码(除了生成干净的渲染 html)。 To assist in satisfying my quest, I'll use template strings with
printf()
and vsprintf()
.为了帮助满足我的要求,我将使用带有
printf()
和vsprintf()
模板字符串。
<?php
$tr = <<<HTML
<tr style="color: %s;">%s
</tr>
HTML;
$tds = str_repeat("\n " . '<td>%s</td>', 4);
?>
<table>
<?php
foreach ($colors as $key => $color) {
printf(
$tr,
$color,
vsprintf(
$tds,
range($key, $key * 4, $key)
)
);
}
?>
</table>
Unrendered Output:未渲染输出:
<table>
<tr style="color: #32CD32;">
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr style="color: #FF0000;">
<td>2</td>
<td>4</td>
<td>6</td>
<td>8</td>
</tr>
<tr style="color: #5E0087;">
<td>3</td>
<td>6</td>
<td>9</td>
<td>12</td>
</tr>
<tr style="color: #FFA500;">
<td>4</td>
<td>8</td>
<td>12</td>
<td>16</td>
</tr>
<tr style="color: #00008B;">
<td>5</td>
<td>10</td>
<td>15</td>
<td>20</td>
</tr>
</table>
Using pure CSS is the more professional technique for styling.使用纯 CSS 是更专业的样式技术。 Once you move the css to an external stylesheet, your php script will be very short and easy to manage.
一旦您将 css 移动到外部样式表,您的 php 脚本将非常简短且易于管理。
Code with the same output: ( Demo )具有相同输出的代码:(演示)
<style>
table {
border-collapse: collapse;
width: 1%;
}
tr:nth-child(1) { color: #32CD32; }
tr:nth-child(2) { color: #FF0000; }
tr:nth-child(3) { color: #5E0087; }
tr:nth-child(4) { color: #FFA500; }
tr:nth-child(5) { color: #00008B; }
td {
border: solid 1px black;
padding: 8px;
}
</style>
<?php
$tr = <<<HTML
<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>
HTML;
?>
<table>
<?php
for ($i = 1; $i <= 5; ++$i) {
vprintf(
$tr,
range($i, $i * 4, $i)
);
}
?>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.