【问题标题】:Background img based on Current Month基于当前月份的背景图片
【发布时间】:2013-05-03 08:36:52
【问题描述】:

使用php,如何根据当月更改我网站上的背景图片?

一月 = 01.jpg

二月 = 02.jpg

等等……

目前我在一个单独的目录中使用“rotator.php”,以便在每次刷新时获得不同的图像。

css 文件我有这个:

身体

{

背景:url(../images/theme/backdrop/rotator.php);

显然链接到 rotator.php.... 我想用 index.php 替换 rotator.php,但不确定使用什么代码来调用图像。

这就是我目前所拥有的......

 <?php


 // Image File Ext.

$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';

//today and Image to use per today

$today = date('m');
printf('$today');

if ($today == 01) {
    $image = "01.jpg";
}
elseif ($today == 02) {
    $image = "02.jpg";
}
elseif ($today == 03) {
    $image = "03.jpg";
}
elseif ($today == 04) {
    $image = "04.jpg";
}
elseif ($today == 05) {
    $image = "05.jpg";
}
elseif ($today == 06) {
    $image = "06.jpg";
}
elseif ($today == 07) {
    $image = "07.jpg";
}
elseif ($today == 08) {
    $image = "08.jpg";
}
elseif ($today == 09) {
    $image = "09.jpg";
}
elseif ($today == 10) {
    $image = "10.jpg";
}
elseif ($today == 11) {
    $image = "11.jpg";
}
elseif($today == 12) {
    $image = "12.jpg";
}

?>

不确定这是否朝着正确的方向发展......

【问题讨论】:

  • 为什么会有java标签?
  • 许多不同的可能解决方案。你有到目前为止你尝试过的代码示例吗?
  • 提示$month = date("m");
  • PHP 有一个date() 函数:ca2.php.net/manual/en/function.date.php
  • 您将赋值运算符= 与相等运算符== 混淆了-您的if 语句都需要采用if ($month == 01) 的形式,否则它将始终选择第一个选项。

标签: php css background


【解决方案1】:

简单的事情就可以搞定:

printf('<html class="month-%s">', date('m'));

然后在 CSS 中:

body {background: /* default background style */}
    .month-01 body {background-image: url('01.jpg')}
    .month-02 body {background-image: url('02.jpg')}
    ...

这使您不仅可以根据月份设置正文样式,还可以设置网站上的所有其他元素。

【讨论】:

  • 检查我上面尝试的代码......让我知道我错在哪里......我想我需要一些东西来获得这个月......
  • @C.Smith:为什么这么复杂?你不能改变 HTML 的 class 属性吗?这更有效(从长远来看,您可能希望缓存 CSS)。
  • 使用一个简单的机器论坛作为我的基础,只是试图操纵已经存在的东西以获得预期的效果你在上面看到的文件是我自己尝试的......我指导了 css在系统的主题文件中查找上述文件...让我从创建的 php 中控制所需的背景...我认为...大声笑
  • printf('', date('m'));
【解决方案2】:

不确定您是如何设置自己的,但想到的最简单的方法是将 php 文件链接为样式表

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

在styles.php上

<?php
header("Content-type: text/css");
?>
body {
  background: url('<?= date('m') ?>.jpg'); // 'm' is number month (01 through 12) 
}

干杯!

【讨论】:

  • 我有 index.css 链接到图像目录中的 rotator.php.... 打算使用目录中的 index.php 来让它选择正确的图像....
  • 你的意思是你有这样的东西? background: url('rotator.php'); 如果是这样,您需要 rotator.php 为您要使用的图像格式传递正确的标题。
  • body { 背景: url(../images/theme/backdrop/rotator.php);
  • 对不起,我只是想创建一个新的 .php 文件来从目录中选择图像以每月发布一次......请耐心等待......哈哈
  • 调整了我原来的帖子......让我知道......我想我错过了一个月的东西......无论是服务器端还是用户端......
猜你喜欢
  • 1970-01-01
  • 2019-01-15
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
相关资源
最近更新 更多