简体   繁体   English

如何在乘法表php中使用for循环将5种文本颜色放在5行中?

[英]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'>&nbsp$num&nbsp</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'>&nbsp$num&nbsp</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]; 
  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" ];
  2. 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>
  3. 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()模板字符串。

Code: ( Demo )代码:(演示

<?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>

呈现的 HTML


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.

相关问题 如何使用 php 将用户输入放入文本文件? - How do i put userinput into text file using php? 如何将循环放入html表? - how do I put loop into html table? 如何使用 PHP 为交替的表格行提供不同的背景颜色 - How to give alternating table rows different background colors using PHP 如何在php中创建:2种颜色的表行,3种颜色的字体 - How to create in php: 2 colors table rows with 3 colors font 如何使用 php 在 input type=&quot;text&quot; 中放入文本(值) - How do I put a text (value) inside an input type="text" using php 我如何将所有行从 php 放入电子邮件? - How do i put all rows from php to the email? 我如何遍历隐藏字段项并使用PHP放入会话 - How do i Loop through the hidden field items and put in Session using PHP 如何在 while 循环中为交替表行提供不同的背景 colors 和 CSS(不使用:nth-child() 选择器) - How to give alternating table rows different background colors in while loop and CSS (not using :nth-child() selector) 我正在使用 php for loop 以简写方式创建动态年份按钮,我如何将活动 class 放入 for 循环中 - I'm create dynamic year button using php for loop in shorthand ,How do i put active class in for loop HTML / PHP:如何突出显示在我生成的乘法表中搜索到的数字? - HTML / PHP :how do I highlight the number searched in multiplication table that i generated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM