我正在尝试根据星期几更改div的背景图像。图像位于名为“img”的文件夹中,我有以下代码;
HTML
$submenu['themes.php'][6][0]
css(外部样式表)
<?php
$images_by_day = array('monday.jpg', 'tuesday.jpg', 'wednesday.jpg', ...);
$image_for_today = $images_by_day[date('w')];
?>
<div class="jumbotron indx-BG">
//content
</div>
答案 0 :(得分:2)
最好在css文件中定义具有不同背景图像的七个不同类,然后使用php文件在元素中应用这些类。
像:
<div class="container <?php echo date('l')?>-bg" id="indx-jumbo">
//content
</div>
在你的css文件中你定义了css类,如:
.monday-bg {
background: url('../img/image_for_monday.jpg') no-repeat center center;
}
.tuesday-bg {
background: url('../img/image_for_tuesday.jpg') no-repeat center center;
}
.
.
.
.sunday-bg {
background: url('../img/image_for_sunday.jpg') no-repeat center center;
}
答案 1 :(得分:1)
您不应该在CSS文件中使用PHP。解决此问题的一种方法是通过内联添加背景图像,如下所示:
<div class="container" id="indx-jumbo" style="background-image: url('../img/<?php echo $image_for_today ?>.jpg')">
//content
</div>
答案 2 :(得分:0)
您不能在css文件中使用PHP。您可以添加内联background image
。请检查以下代码。
<div class="container" id="indx-jumbo" style="background-image: url('../img/<?php echo date( "l", $timestamp); ?>.jpg')">
//content
</div>
答案 3 :(得分:0)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"NavDashBoardToInfo"])
{
InfoViewController *info = [segue destinationViewController];
info.AppointmentDictionary = "Value you want to pass";
}
}
答案 4 :(得分:0)
尝试这个。
<?php
$images_by_day = array('monday.jpg', 'tuesday.jpg', 'wednesday.jpg', 'thursday.jpg','friday.jpg','saturday.jpg','sunday.jpg');
$today = date('l');
foreach ($images_by_day as $key => $value) {
$day = substr($value,0,-4);
$str = strcasecmp($day, $today);
if($str == '0'){
$background = $value;
}
}
?>
<div class="container" id="indx-jumbo" style="background-image: url('../img/<?= $background; ?>');">
//content
</div>
答案 5 :(得分:0)
您需要检查您的图片名称是否包含一周中的某一天。你可以使用php的子串内置函数轻松完成。
<?php
foreach($imagArray as $image)
{
if(strpos($image,date('l'))
{
//display image and exit
}
}
?>