Hello,
My situation:-
I was using the code below to change the background image of my web pages every month. The code was working fine for many months. It has suddenly stopped working though I can't figure out why. Could you help me debug this or propose a more reliable code?

My code is below:-

<?php
$festival = date('m');
echo $festival;
if ($festival == 10){echo "<body style=background-color: #CCFFFF background=deepavali2.gif>";}
if ($festival == 01){echo "<body style=background-color: #CCFFFF background=cny2.gif>";}
if ($festival == 12){echo "<body style=background-color: #CCFFFF background=christmas2.jpg>";}
if ($festival == 09){echo "<body style=background-color: #CCFFFF background=hari_raya2.jpg>";}
if ($festival == 08){echo "<body style=background-color: #CCFFFF background=merdeka.jpg>";}
if ($festival == 06){echo "<body style=background-color: #CCFFFF background=agung.jpg>";}
else{echo "<body style=background-color: #CCFFFF background=klcc1.jpg>";}
?>

Additional Info:-
The gifs & jpegs for the background images are definitely present in the same directory as the web pages.
I am using the same server as before.
My web host did some upgrading last month though I don't know exactly what they upgraded.
I've attached a screen shot to show you how my web page looks like now. The background image that you see is klcc1.jpg. It is supposed to have changed to merdeka.jpg for this month but it has not.

Thank you very much for your advice, time & trouble.

    Sorry, I don't understand what you mean. Thanks for your reply anyway.

      if ($festival == 09){echo '<body style="background-color: #CCFFFF; background=hari_raya2.jpg">';} 

      I think that is right from inline css, try that syntax on all your code.

        No.

        Okay, each code block SHOULD look like this, for a start:

        if ($festival == 09){echo '<body style="background-color: #CCFFFF;" background="hari_raya2.jpg">';} 
        

        Secondly, when you view the page source of the website, you actually have TWO <body> tags.

        <body style="background-color: #CCFFFF">
        08<body style=background-color: #CCFFFF background=klcc1.jpg>
        

        The second one automatically gets discarded by the browser.

          I think that whole code needs a rethink. Why not use css to the background picture also? (not background="blaablaa.jpg"). Also this is job for a switch statement

          How about:

          <?php
          $festival = date('n'); // Use n instead of m. No leading zeros.
          
          switch($festival) {
          	case 1:
          	$bg = 'cny2.gif';
          	break;
          
          case 6:
          $bg = 'agung.jpg';
          break;
          
          case 8:
          $bg = 'merdeka.jpg';
          break;
          
          case 9:
          $bg = 'hari_raya2.jpg';
          break;
          
          case 10:
          $bg = 'deepavali2.gif';
          break;
          
          case 12:
          $bg = 'christmas2.jpg';
          break;
          
          default: 
          $bg = 'klcc1.jpg'; 
          break;
          }
          
          echo '<body style="background: #CCFFFF url('.$bg.');">';
          ?>
          

          Or with array:

          $festival = date('n'); // Use n instead of m. No leading zeros.
          
          $p = array( 1 =>'cny2.gif',
          			6 =>'agung.jpg',
          			8 =>'merdeka.jpg',
          			9 =>'hari_raya2.jpg',
          			10=>'deepavali2.gif',
          			12=>'christmas2.jpg');
          
          if (array_key_exists($festival,$p))
          	$bg = $p[$festival];
          else
          	$bg = 'klcc1.jpg';
          
          echo '<body style="background: #CCFFFF url('.$bg.');">';
          

            Thank you bike5 & madwormer2 for your suggestions. For some strange reason, both your suggestions did not work. Nevertheless, I appreciate your willingness to help.

            Thank you cahva for your guidance. Your example works very well :-) I appreciate your generosity & guidance on this matter.

            As for using css, I don't know what this is all about but nevertheless I'll do some reading on css.

            Thank you once again.

              It's showing the correct background here (merdeka.jpg)..........perhaps it's a browser caching issue.

                ClarkF1 wrote:

                It's showing the correct background here (merdeka.jpg)..........perhaps it's a browser caching issue.

                Or maybe you're just a bit too late? Thread already says resolved.

                  Thanks for your reply, ClarkF1. I think you visited my page after I edited the code using cahva's example. Nevertheless, I thank you for your observation & wish you the very best.

                    Write a Reply...