【问题标题】:Problems with CSS styling in PHP codePHP代码中的CSS样式问题
【发布时间】:2014-11-23 03:27:39
【问题描述】:

首先,我对 PHP 很陌生,所以可能会有一些菜鸟错误。 总之;

我在这里有一个目标,就是制作一个基本上是日历的表格。然后我希望表格中对应于当天的单元格有红色文本。就这些。但它不起作用。 这是我的代码:

<table border="1" id="calendar">

        <caption><h1>September</h1></caption>
        <?php



            $currentDate = (int)date("d");
            $currentMonth = (int)date("m");
            $currentMonthL = date("L");
            $columnsInRow = 7;
            $daysInMonth = date("t");

            $currentDay = 1;
                for ($i=0 ; $i < $daysInMonth ; $i++) {
                    ECHO '<tr class="cal">';
                        for ($j=0 ; $j < $columnsInRow && $currentDay <= $daysInMonth ; $j++) {
                            ECHO var_dump($currentDay);
                            ECHO var_dump($currentDate);
                            if(currentDate==currentDay) {
                                ECHO '<div style="height:150%;width:100%text-decoration:none;"><td class="cal"><a href="date_info.php?currentDay=' . $currentDay . '"><h2 style"color:red;">' . $currentDay . '</h2></a></td></div>';
                                $currentDay++;
                            }else{
                                ECHO '<div style="height:150%;width:100%text-decoration:none;"><td class="cal"><a href="date_info.php?currentDay=' . $currentDay . '"><h2 style="color:#554100;">' . $currentDay . '</h2></a></td></div>';
                                $currentDay++;                              
                            }
                        }
                    ECHO '</tr>';
                }           
        ?>

    </table>

这是它给我的(注意当天不是红色的):

提前谢谢你

【问题讨论】:

  • 您将 CSS 应用于 HTML,而不是 PHP。查看您正在生成的 HTML。这是你所期望的吗?这当然是无效的。使用a validator
  • 你缺少 = in style"color:red;"

标签: php html css colors styles


【解决方案1】:

你的代码有两个问题。

php if 子句中的第一个if(currentDate==currentDay) 需要更正为if($currentDate == $currentDay)

css 标记中的第二个 &lt;h2 style"color:red;"&gt; 必须更正为 &lt;h2 style="color:red;"&gt;

希望对你有帮助

【讨论】:

  • 虽然这会产生正确的标记,但条件仍然不会触发。
  • 好的,但这也是个问题。
【解决方案2】:

其实很简单。

你实际上并没有比较你的变量。仔细看看。

if(currentDate===currentDay) 

应该是

if($currentDate===$currentDay) 

Here's an eval.in illustrating this

【讨论】:

  • if(currentDate===currentDay) 不在代码中,没有 css 校正仍然不会触发红色。
  • @emmanuel 你瞎了吗? if(currentDate==currentDay) { ECHO '&lt;div 再看一遍。
  • 我不这么认为,请检查 (==) 而不是 (===)。
  • @emmanuel 不,directly equals to comparison 很好。我不确定你怎么看不到比较两个从未声明过的常量是行不通的。请停下来。
  • 好的,但在 OP 代码中,您的代码中有 2 = 而不是 3 =。
猜你喜欢
  • 2014-05-08
  • 2012-11-25
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多