What is a better way to write this more efficiently....in php

Hello All:

I am writing a script for my job where a graphic is going to change based on the season or date we are in.

So I am doing this,

<?php
    
    if(time() > 1262930400 && time() < 1263427199){

        echo ('<tr><td align="center" valign="middle" colspan="2"><a href="acrylic-raffle-drums.htm"><img src="images/raffledrums_tn.jpg" border="0" hspace="0" vspace="5" width="238" height="98" alt="Giant Raffle Drums"></a></td></tr>');
    
    }else{
    
    echo ('<tr><td align="center" valign="middle" colspan="2">&nbsp;</td></tr>');    
    }
    
?>

However, I have 11 of these conditions to change and I don’t want to do what I am doing now which is have 11 else’s down the page.

Is there another more efficient way to do this?

Thanks for any help or advice on this,

Paul

I would separate the logic from the html more like this way…


<?php    if(CheckCondition(1)){ ?> 

<tr><td align="center" valign="middle" colspan="2"><a href="acrylic-raffle-drums.htm"><img src="images/raffledrums_tn.jpg" border="0" hspace="0" vspace="5" width="238" height="98" alt="Giant Raffle Drums"></a></td></tr>

<?php } ?> 
..etc

But it depends on how much of the html is to change for each condition and so…
maybe this approach could work



<tr><td align="center" valign="middle" colspan="2">
<a href="acrylic-raffle-drums.htm">
<?php print GetImage()?></a>
</td></tr>


the HTML will only change the image name and the alt tag.

I will have to look up CheckCondition as I am new to php functions, never used that before.

Thanks Danman…

graphic is going to change based on the season or date we are in

It would also be helpful to have 3-4 examples of conditions you will be using. This way we can better judge exactly how you plan to approach this problem. Then we will be able to better judge the most efficient code.

Sure thing, Here is how it is going so far on my page


<?php
    
    if(time() > 1293148800 && time() < 1262390340){

        echo ('<tr><td align="center" valign="middle" colspan="2"><a href="decorations-party-accessories.html"><img src="images/new-years_tn.jpg" border="0" hspace="0" vspace="5" width="238" height="98" alt="New Years Eve Celebration"></a></td></tr>');
    
    }else{
    
    echo ('<tr><td align="center" valign="middle" colspan="2">&nbsp;</td></tr>');    
    }
    
    if(time() > 1262390400 && time() < 1265068740){

        echo ('<tr><td align="center" valign="middle" colspan="2"><a href="decorations-party-accessories.html"><img src="images/seasonal_valentines.jpg" border="0" hspace="0" vspace="5" width="238" height="98" alt="Valentines Day Decorations"></a></td></tr>');
    
    }else{
    
    echo ('<tr><td align="center" valign="middle" colspan="2">&nbsp;</td></tr>');    
    }
    
    if(time() > 1265068800 && time() < 1266364740){

        echo ('<tr><td align="center" valign="middle" colspan="2"><a href="decorations-party-accessories.html"><img src="images/seasonal_mardi-gras.jpg" border="0" hspace="0" vspace="5" width="238" height="98" alt="Mardi Gras Fat Tuesday Decorations"></a></td></tr>');
    
    }else{
    
    echo ('<tr><td align="center" valign="middle" colspan="2">&nbsp;</td></tr>');    
    }
    
    if(time() > 1266364800 && time() < 1268870340){

        echo ('<tr><td align="center" valign="middle" colspan="2"><a href="decorations-party-accessories.html"><img src="images/seasonal_st-pats-day.jpg" border="0" hspace="0" vspace="5" width="238" height="98" alt="St Patricks Day Decorations"></a></td></tr>');
    
    }else{
    
    echo ('<tr><td align="center" valign="middle" colspan="2">&nbsp;</td></tr>');    
    }

So basically, when a certain date comes up, a graphic that I have placed will change.

Make yourself up a little array


$conditions = array(
..array(
....'time1' => 1293148800, 
....'time2' => 1262390340,
....'image' => 'new-years_tn.jpg',
....'alt' => 'Happy New Years'
..),
..array( // repeat for each condition
);

After that it becomes a case of cycling through the array until the time condition is met and then pulling out the image values.
Or use a database.

Is there a reason you are set on using the time() function? I might suggest you use the date function.

Some code like this could go a long ways.

I would start by creating an array of months, img, and alt text

<?php
//This is just an example so you would of course include all the months
$constraints = array("Jan"=>array("image1.jpg", "Alt Text For Image 1"), "Feb"=>array("image2.jpg", "Alt Text For Image 2"), "Mar"=>array("image3.jpg", "Alt Text For Image 13")); 

$currentMonth = date(M);

$text = '<tr><td align="center" valign="middle" colspan="2"><a href="decorations-party-accessories.html"><img src="images/' . $constraints[$currentMonth][0] . '" border="0" hspace="0" vspace="5" width="238" height="98" alt="' . $constraints[$currentMonth[1] . '"></a></td></tr>';
?>

I’ve not tested this so I do not know if it actually works, just thinking aloud here.

Thanks guys for all your input on this, I will have to review my options and test and play.

Thanks for the head start though and ideas on this.

I do not know why I am using the time() function, it was the one I chose at the time. The date one makes more sense or until I realize why I could not use it. LOL