iconicCreator Posted May 19, 2009 Share Posted May 19, 2009 I have managed to create a script from reading and practicing, the script changes or display random images every time the page is reloaded or the browser is refreshed. The codes are in an include folder because I like to separate certain PHP script from HTML, this way I can have some stuff in separate places for easier trouble shooting. My question is or should I say, I need the caption that display along with the images to also display the time of the day. Example: Good morning Bob, if the person is visiting the page in the morning. Or Good afternoon Bob, if someone is visiting the page in the afternoon and so on. This is what I have so far. <?php $images = array( array('file' => 'image1', 'caption' => 'Good morning, <br /> my name is Rick, how may I help you?'), array('file' => 'image2', 'caption' => 'Good morning, <br /> my name is Dianna, how may I help you'), array('file' => 'image3', 'caption' => 'Good morning, <br /> my name is David, how may I help you'), array('file' => 'image4', 'caption' => 'Good morning, <br /> my name is Mary, how may I help you'), ); $i = rand(0, count($images)-1); $selectedImage = "images/{$images[$i]['file']}.png"; $caption = $images[$i]['caption']; ?> The script above is saved in an include folder. This is the HTML portion: <?php include('includes/random_image.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <link href="assets/journey.css" rel="stylesheet" type="text/css" media="screen" /> </head> <body> <img src="<?php echo $selectedImage; ?>" alt="Random image" /> <p class="caption"><?php echo $caption; ?></p> </body> </html> How do I get this going? Where do I start? What I want to do is new to me, I am still a novice! Any ideas or direction? Thanks everyone! IC Quote Link to comment Share on other sites More sharing options...
void Posted May 19, 2009 Share Posted May 19, 2009 Well, if it was up to me, first I'd replace word "morning" in $images array with something like "[PARTOFDAY]". Then, I'd create a function, which, according to time, replaces [PARTOFDAY] with the corresponding part. it would look something like that: function PartOfDay($string) { $hour = date("H",time()); if($hour >= 6 && $hour < 12) $replace = 'morning'; elseif($hour >= 12 && $hour < 18) $replace = 'afternoon'; elseif($hour >= 18 && $hour < 24) $replace = 'evening'; else $replace = 'god knows what'; return str_replace("[PARTOFDAY]",$replace,$string); } and, to make this work, you'll just write <?php echo PartOfDay($caption); ?> hope I got this right, good luck Quote Link to comment Share on other sites More sharing options...
iconicCreator Posted May 19, 2009 Author Share Posted May 19, 2009 Well, if it was up to me, first I'd replace word "morning" in $images array with something like "[PARTOFDAY]". Then, I'd create a function, which, according to time, replaces [PARTOFDAY] with the corresponding part. it would look something like that: function PartOfDay($string) { $hour = date("H",time()); if($hour >= 6 && $hour < 12) $replace = 'morning'; elseif($hour >= 12 && $hour < 18) $replace = 'afternoon'; elseif($hour >= 18 && $hour < 24) $replace = 'evening'; else $replace = 'god knows what'; return str_replace("[PARTOFDAY]",$replace,$string); } and, to make this work, you'll just write <?php echo PartOfDay($caption); ?> hope I got this right, good luck Hey thank you so much for the time and the effort! I have not worked with the code yet but I will try this and let you know how things come out. Your effort is all that counts and I really appreciate it! Thanks again - will get back to you! IC Quote Link to comment Share on other sites More sharing options...
iconicCreator Posted May 20, 2009 Author Share Posted May 20, 2009 Hey there! Just wanted to get back to you on this. It works! Yeah! I had to do little changes in terms of making some corrections with the code you showed me, there were inconsistency with some of the naming. Eg: PARTOFDAY and PartOfDay, not the same. Anyway, it works perfectly! You've been a very great help! Have a very wonderful day! IC Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.