HiDayurie Dave
HiDayurie Dave

Reputation: 1807

PHP Change Background Image Everyday Based on Day from Database

I have an PHP code to set daily background page change(everyday change based on day name from database)

On my table

BACKGROUND_DAY | BACKGROUND_IMAGE
Monday         | White.png
Tuesday        | Black.png
Wednesday      | Yellow.png
\\until Sunday

Then I query it:

$day = date("l");

$qBackground = oci_parse($c1, "SELECT * FROM WA_GA_TBL_BACKGROUNDS");
oci_execute($qBackground);
while($dBackground = oci_fetch_array($qBackground))
{
   $backgroundDayArray[] = $dBackground['BACKGROUND_DAY'];
   $backgroundImageArray[] = $dBackground['BACKGROUND_IMAGE'];
}

//print_r backgroundDayArray
Array ( [0] => Saturday [1] => Wednesday [2] => Friday [3] => Tuesday [4] => Thursday [5] => Monday [6] => Sunday ) 

$bg_color =  isset($backgroundImageArray[$day]) ? $backgroundImageArray[$day] : 'black';

and set it to HTML

<style>
html
{
    background: url(assets/img/background/<?php echo $bg_color; ?>) no-
    repeat center center fixed;
}
</style>

And the result is the page is show nothing.

What I want is,

Set the background image based on table day. If today is Monday then set it to background image for Monday.

Any idea?

Upvotes: 0

Views: 295

Answers (2)

Rotimi
Rotimi

Reputation: 4825

You do not need to even have a while loop here. When you query your database pass the current date in your query using PHP's date('l'). This would dynamically get the color for the current date.

$day = date('l'); 
$query = "select BACKGROUND_IMAGE from WA_GA_TBL_BACKGROUNDS where BACKGROUND_DAY = $day";

Make sure the page where you have echo the current background image has a .php extension. Also make sure the path exists.

Upvotes: 1

user8461611
user8461611

Reputation:

$backgroundImageArray is an associative array which has ints as keys. Thus, $backgroundImageArray[$day] will always be unset and your condition will result in black. print_r($backgroundImageArray) will give you the hint. In your while-loop, replace the array statements with

$backgroundImageArray[$dBackground['BACKGROUND_DAY']] = $dBackground['BACKGROUND_IMAGE'];

and it should work.

Upvotes: 0

Related Questions