Jump to content

what if? is it poissble?


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.