Guest Posted March 21, 2008 Share Posted March 21, 2008 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 More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 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; ?> Link to comment https://forums.phpfreaks.com/topic/97257-what-if-is-it-poissble/#findComment-497664 Share on other sites More sharing options...
Guest Posted March 21, 2008 Share Posted March 21, 2008 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 Link to comment https://forums.phpfreaks.com/topic/97257-what-if-is-it-poissble/#findComment-497670 Share on other sites More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 Ah, even easier... <?php $image_dir = 'images/'; $img = date('G').'00.jpeg'; if(!file_exists($image_dir).$img) $img = 'default.jpeg'; echo $img; ?> Link to comment https://forums.phpfreaks.com/topic/97257-what-if-is-it-poissble/#findComment-497673 Share on other sites More sharing options...
Guest Posted March 21, 2008 Share Posted March 21, 2008 thanks small neat code. very helpful thank u Link to comment https://forums.phpfreaks.com/topic/97257-what-if-is-it-poissble/#findComment-497678 Share on other sites More sharing options...
Orio Posted March 21, 2008 Share Posted March 21, 2008 Fixing the small error rhodesa has in his code: <?php $image_dir = 'images/'; $img = date('G').'00.jpeg'; if(!file_exists($image_dir.$img)) $img = $image_dir.'default.jpeg'; echo '<img src="$img" />; ?> Orio. Link to comment https://forums.phpfreaks.com/topic/97257-what-if-is-it-poissble/#findComment-497679 Share on other sites More sharing options...
kenrbnsn Posted March 21, 2008 Share Posted March 21, 2008 Shortened code (with a few typos fixed): <?php $fn = 'images/' . date('G') . '00.jpeg'; $img = (file_exisits($fn))?$fn:$image_dir.'default.jpeg'; echo '<img src="' . $img . '" />'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/97257-what-if-is-it-poissble/#findComment-497685 Share on other sites More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 more fixes.... <?php $image_dir = 'images/'; $fn = $image_dir . date('G') . '00.jpeg'; $img = (file_exisits($fn))?$fn:$image_dir.'default.jpeg'; echo '<img src="' . $img . '" />'; ?> Link to comment https://forums.phpfreaks.com/topic/97257-what-if-is-it-poissble/#findComment-497710 Share on other sites More sharing options...
Guest Posted March 21, 2008 Share Posted March 21, 2008 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 Link to comment https://forums.phpfreaks.com/topic/97257-what-if-is-it-poissble/#findComment-497728 Share on other sites More sharing options...
BlueSkyIS Posted March 21, 2008 Share Posted March 21, 2008 file_exists Link to comment https://forums.phpfreaks.com/topic/97257-what-if-is-it-poissble/#findComment-497729 Share on other sites More sharing options...
Guest Posted March 21, 2008 Share Posted March 21, 2008 great work working 100% cheers Link to comment https://forums.phpfreaks.com/topic/97257-what-if-is-it-poissble/#findComment-497744 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.