kate_rose Posted January 28, 2013 Share Posted January 28, 2013 Hi, I haven't done that much in php so thanks for you patience if there are really obvious errors or bad practices I am trying to rotate the content of a div depending on the date between 3 different images (each day a new image is displayed and after 3 days it starts over - in the future we may need to add more images). For some reason my code always displays the second image of the three no matter what the day is. Here is my code. <?php $num_slides=3; //let the variable num_slides = the number of slides to be shown (in this case 3) $day_month = date('d'); //load the day of the month into $day_month $slidenum = $day_month % $num_slides; //let $remain = the remainder after $daymonth is divided by $num_slides $slidenum = 0; //override normal $slidenum generation for debugging if ($slidenum=0) {echo <<<EOL <div id="learning_outside_top"> <a href="learning_outside/learning_outside_main.php" title="link to hands on learning page" alt="link to hands on learning page"><img src="images/learning_outside_research_top.jpg" onmouseenter="this.src='images/learning_outside_research_top_mouseover.jpg'" onmouseleave="this.src='images/learning_outside_research_top.jpg'"/></a> </div> <a href="learning_outside/learning_research.php" title="link to undergraduate research FAQ page" alt="link to undergraduate research FAQ page"><img src="images/learning_outside_research_bottom.jpg"/></a> EOL; ; } elseif ($slidenum=1) {echo <<<EOD <div id="learning_outside_top"> <a href="learning_outside/learning_outside_main.php" title="link to hands on learning page" alt="link to hands on learning page"><img src="images/learning_outside_internships_top.jpg" onmouseenter="this.src='images/learning_outside_internships_top_mouseover.jpg'" onmouseleave="this.src='images/learning_outside_internships_top.jpg'"/></a> </div> <a href="learning_outside/learning_internships.php" title="link to undergraduate internship FAQ page" alt="link to undergraduate internship FAQ page"><img src="images/learning_outside_internships_bottom.jpg"/></a> EOD; ; } else {echo <<<EOC <div id="learning_outside_top"> <a href="learning_outside/learning_outside_main.php" title="link to hands on learning page" alt="link to hands on learning page"><img src="images/learning_outside_class_based_top.jpg" onmouseenter="this.src='images/learning_outside_class_based_top_mouseover.jpg'" onmouseleave="this.src='images/learning_outside_class_based_top.jpg'"/></a> </div> <a href="learning_outside/class_based.php" title="link to undergraduate hands on learning in classes page" alt="link to undergraduate hands on learning in classes page"><img src="images/learning_outside_class_based_bottom.jpg"/></a> EOC; ; } ?> I am sure that the $slidenum part of this is working correctly because I have echoed that. Even when I insert a line that sets $slidenum = 0; (like in the code above) it still only shows the second image which is created by the echo EOD section of the code. It is probably just something I am missing after staring at this too long but I could really use another set of eyes. Thanks, Kate Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 28, 2013 Share Posted January 28, 2013 = is assignment. == is comparison. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 28, 2013 Share Posted January 28, 2013 (edited) When you are testing if something is equal to something else you need to use DOUBLE equal signs. A single equal sign is an assignment operator. Although, I would expect incorrect results - I wouldn't expect the results you are getting. As for your code, you should not duplicate a lot of redundant code int he if/elseif/else statements. In this case you should only use that logic to DEFINE the image. Additionally, if the number of images will be dynamic, instead of hard-coding the number of images, create an array of images and let the code figure it out programatically. Since all your images/links have a set format you could just use code to set the "root' word (e.g. 'internship', 'research', or 'classes'), but I would define all the elements separately so you are not tied to that in the future. Lastly, rather than use the day of the month, a better option (IMHO) is the day of the year (i.e. 0 to 365) <?php //Define the elements of the various slides //This is the ONLY code that would need to be modified if slides are added in the future. //Also, this can be stored in a separate file and included OR stored in a DB $slides = array( array( //internships 'page' => 'learning_internships.php', 'text' => 'link to undergraduate internship FAQ page', 'top' => 'learning_outside_internships_top_mouseover.jpg', 'bottom' => 'learning_outside_internships_bottom.jpg', 'over' => 'learning_outside_research_top_mouseover.jpg' ), array( //research 'page' => 'learning_research.php', 'text' => 'link to undergraduate research FAQ page', 'top' => 'learning_outside_research_top.jpg', 'bottom' => 'learning_outside_research_bottom.jpg', 'over' => 'learning_outside_research_top_mouseover.jpg' ), array( //class based 'page' => 'class_based.php', 'text' => 'link to undergraduate hands on learning in classes page', 'top' => 'learning_outside_class_based_top.jpg', 'bottom' => 'learning_outside_class_based_bottom.jpg', 'over' => 'learning_outside_class_based_top_mouseover.jpg' ) ); //Determine which slide to use $slideNum = date('z') % count($slides); //Define the slide to use $slide = $slides[$slideNum]; //Create the output echo <<<EOL <div id="learning_outside_top"> <a href="learning_outside/learning_outside_main.php" title="link to hands on learning page" alt="link to hands on learning page"> <img src="images/{$slide['top']}" onmouseenter="this.src='images/{$slide['over']}'" onmouseleave="this.src='images/{$slide['top']}'"/> </a> </div> <a href="learning_outside/{$slide['page']}" title="{$slide['text']}" alt="{$slide['text']}"> <img src="images/{$slide['bottom']}"/> </a> EOL; ?> Edited January 28, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
kate_rose Posted January 28, 2013 Author Share Posted January 28, 2013 Thank you Jessica & Psycho. The loop is working correctly now - I hadn't written one like this before & didn't realize the dif. between = & ==. Psycho, I think day of the year is probably better because the first 2 days of the month will display the same slide. The images won't be dynamic & the max will be 5 so I probably won't need a DB. & it is a good idea to use an array (I do with some of my other bits on this site). I appreciate the advice, I can obviously use it. Kate Quote Link to comment Share on other sites More sharing options...
kate_rose Posted January 28, 2013 Author Share Posted January 28, 2013 P.S. Psycho it is weird that it always displayed the second slide when it broke . . . Oh well I have to much to do to figure out why right now. K- 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.