Mali
Mali

Reputation: 131

PHP image swap based on day and url

My goal is to have a background image swapped out according to the day of the week and to the link the user came from.

example:

Monday, index.php = bg1.jpg

Monday, about.php = bg2.jpg

Tuesday, index.php = bg3.jpg

Tuesday, about.php = bg4.jpg

This is my switch that handles the case of the week but not sure how to go from here:

<?php

// Variables
$url = "images/days/";
$monImage = "$url"."bg_mon_a.jpg";
$tueImage = "$url"."bg_tue_a.jpg";
$wedImage = "$url"."bg_wed_a.jpg";
$thurImage = "$url"."bg_thu_a.jpg";
$friImage = "$url"."bg_fri_a.jpg";
$weekendImage = "$url"."bg_default.jpg";
$d = date("D");
//Function that switches between date images based on the actual day of the week in $d
switch ($d)
{
case Mon:
echo "<img id='bg_image' src=$monImage>\n";
break;
case Tue:
echo "<img id='bg_image' src=$tueImage>\n";
break;
case Wed:
echo "<img id='bg_image' src=$wedImage>\n";
break;
case Thu:
echo "<img id='bg_image' src=$thurImage>\n";
break;
case Fri:
echo "<img id='bg_image' src=$friImage>\n";
break;
default:
echo "<img id='bg_image' src=$weekendImage>\n";
}
//End
?>

Upvotes: 0

Views: 285

Answers (4)

yitwail
yitwail

Reputation: 2009

if it's a background image you want, maybe you need something like

echo "<div id='div_with_background' style='background: url($weekendImage)'>\n";
echo 'some content';
echo '</div>';

also, if you want to test the link the user came from, do a switch on $_SERVER['HTTP_REFERER'] but remember that $_SERVER['HTTP_REFERER'] is not guaranteed to have the real url the user came from.

Upvotes: 1

Tim Withers
Tim Withers

Reputation: 12059

To get the link clicked to come to your site, you are looking for the HTTP_REFERRER, which is stored in the $_SERVER variables.

Try echoing out: echo $_SERVER['HTTP_REFERRER']; and you will see the website URL that sent the person to your site.

I think this is what you are asking, but if not, you will need to clarify a little more.

Now as far as images go:

$img='bg_'.strtolower(date("D")).'_a.jpg';

Just make sure that you change your weekend image names to sat and sun.

Hope that helps.

Upvotes: 2

Connor Peet
Connor Peet

Reputation: 6265

Try this:

echo "<img id='bg_image' src='images/days/bg_". strtolower(date("D")) ."'>\n";

Upvotes: 1

Luke Pittman
Luke Pittman

Reputation: 843

I think you've over complicated it.

$bgimage = 'bg_' . date ("D") . '.jpg';
echo '<img src="images/' . $bgimage . '">';

Then just make sure you have 7 files, one for each day:

bg_Mon.jpg
bg_Tue.jpg
etc...

Upvotes: 1

Related Questions