Jump to content

what if? is it poissble?


Guest

Recommended Posts

task:

I would like my website to display a differnt image every hour, but if the image cant be found i would like it to display a default image i.e default.jpeg

 

would it be another else statment? everything i have tried has failed please help guys, im using the following  ???

$h = date('G')
if ($h < 3) $img = 'm0200.jpeg';
else if ($h < 4) $img = 'm0300.jpeg';
else if ($h < 5) $img = 'm0400.jpeg';
else if ($h < 6) $img = 'm0500.jpeg';
else if ($h < 7) $img = 'm0600.jpeg';
else if ($h <  $img = 'm0700.jpeg';
else if ($h < 9) $img = 'm0800.jpeg';
else if ($h < 10) $img = 'm0900.jpeg';
else if ($h < 11) $img = 'm1000.jpeg';
else if ($h < 12) $img = 'm1100.jpeg';

Link to comment
https://forums.phpfreaks.com/topic/97257-what-if-is-it-poissble/
Share on other sites

Try a switch statement....

 

<?php
  $image_dir = 'images/';
  switch(date('G')){
    case 0:
    case 1:
    case 2:  $img = 'm0200.jpeg'; break; //This one will apply for 0,1,2
    case 3:  $img = 'm0300.jpeg'; break;
    case 4:  $img = 'm0400.jpeg'; break;
    case 5:  $img = 'm0500.jpeg'; break;
    case 6:  $img = 'm0600.jpeg'; break;
    case 7:  $img = 'm0700.jpeg'; break;
    case 8:  $img = 'm0800.jpeg'; break;
    case 9:  $img = 'm0900.jpeg'; break;
    case 10: $img = 'm1000.jpeg'; break;
    case 11: $img = 'm1100.jpeg'; break;
    default: $img = 'default.jpeg'; break; //Everything else (12-23) will fall in default
  }
  if(!file_exists($image_dir.$img))
    $img = 'default.jpeg';
  print $img;
?>

 

Many thanks for replying so fast. 

 

but.

 

the names of the images will always be the same so i want it to display them if they are in my dir, and if they arent to load the default image.

 

so kinda

 

if 1000.jpg (10 oclock image) is there display it, if not display default.jpg

 

 

tierd latest:

[code<?php
$image_dir = 'screen/';
$fn = $image_dir . 'm' . date('G') . '00.jpg';
$img = (file_exists($fn))?$fn:$image_dir.'default.jpg';
echo '<img src="' . $img . '" />';
?>

 

but

Fatal error: Call to undefined function file_exisits()

 

:(

i put m in as my images are named m1200.jpeg is this why its failing?

 

thanks for all your help guys :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.