php - 跨页面混合 PHP 和 CSS

标签 php css

我有几个页面希望具有相同的背景,所以我认为外部样式表是可行的方法。然而,背景会根据一天中的不同时间发生变化,所以我不得不在 CSS 中混入一些 PHP。所以现在我有一个文件,background.php:

<html>
<style type="text/css">

body
{
    background-image: url('<?php echo (day() == 1 ? 'images/day_sheep.jpg'
                                                : 'images/night_sheep.jpg'); ?>');
    background-position: 50% 50%;
    background-repeat: no-repeat;
    background-color: silver
}

a {text-decoration:none;}
a:link {color:#ff0000;}
a:visited {color:#0000FF;}
a:hover {text-decoration:underline;}

</style>
</html>

从两个不同的页面调用。一个页面工作得很好,但是当我添加行 require_once 'background.php' 时,另一个页面完全崩溃了,我的意思是不再显示任何内容。违规页面如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Penelope's Conquests</title>

    <?php require_once 'background.php'; ?>

    <style type="text/css">

     table {
         margin: 10px;
         margin-left: 50%;
         margin-right: 50%;
         padding: 12px;
         border: 10px;
         text-align: center;
         background-color: #fffb40;
         border-style: ridge;
         border-collapse: separate;
         border-color: #9c6ad6;
         outline-style: inset;
           }

     td.cap {
            text-transform: capitalize;
            font-variant: small-caps;
            }

     td.str {
            font-weight: bolder;
            font-size: 1.4em;
            }

    </style>
</head>
<body>
    <h2>To date, Queen Penelope has destroyed:<br /><br /></h2>


    <?php

    require_once 'victims.php';
    require_once 'mysqlSheep.php';



   echo '<table border="3"
                frame="box"
                cellpadding="5">
         <caption>Penelope\'s Victims.</caption>
         <tr><th>Victim</th><th>Times Zapped</th>';

       $hits = mysql_query("
               SELECT *
               FROM victims
               ORDER BY amount
                          ");

       if( $hits )
       {
            while( $row = mysql_fetch_array($hits) )
            {
                   echo '<tr><td class="cap">'.$row['victim'].'</td>
                         <td>'.$row['amount'].'</td></tr>';
            }
       } 
       else
       {
            echo '<p>' . mysql_error() . '</p>';
       }

   echo '</tr></table><br /><br />';

   echo '<table border="3"
                frame="box"
                cellpadding="5">
         <caption>Button Clicks.</caption>
         <tr><th>Hour of day</th><th>Times Clicked</th>';

   $time = mysql_query("
                       SELECT *
                       FROM time
                       ORDER BY hits
                       ");

   while( $row = mysql_fetch_array($time) )
   {
       print "<tr><td class='str'>".$row['hour']."</td>";
       print "<td>".$row['hits']."</td></tr>";
   }


    ?>
</body>
</html>

为什么页面不想与样式表一起工作?

最佳答案

另一种选择是将您的 css 文件附加到 <link>属性。

所以在你的background.php 地方

<?php
    header("Content-type: text/css");
?>
body
{
    background-image: url('<?php echo (day() == 1 ? 'images/day_sheep.jpg'
                                                : 'images/night_sheep.jpg'); ?>');
    background-position: 50% 50%;
    background-repeat: no-repeat;
    background-color: silver
}

a {text-decoration:none;}
a:link {color:#ff0000;}
a:visited {color:#0000FF;}
a:hover {text-decoration:underline;}

然后调用它

<link rel="stylesheet" type="text/css" href="background.php" />

将此放在 1 个答案中会变得困惑,这就是我添加另一个答案的原因。

关于php - 跨页面混合 PHP 和 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3552438/

相关文章:

php - 使用mysql触发器调用一个php文件

php - 同一个网站,如何将 POST 从 HTTP 发送到 HTTPS?它一直空着

jquery - 读出表单中 <option> 的值并显示同名的 <div>

html - 将褪色黑色分隔符从 "-webkit-gradient"转换为 "linear-gradient"

PHP PDO 将远程主机更改为本地主机名

特征的 PHP instanceof

php - 提交表单时重写 url

html - 如何更改导航丸和导航栏的样式?

不使用 "of-type"的几个容器元素内的 CSS3 目标第一段

css - 如何在 CSS 中重置 <small> 标签