finderya Posted April 21, 2017 Share Posted April 21, 2017 http://www.phpeasystep.com/phptu/22.html Can someone pls help me with this one?My problem is that i have no idea how to add images to php. i tried with div.images in css but nah. Please help! <?php // random number 1 - 100 $result_random = rand(1, 100); // if result less than or equal 70, display ad1 (70%) if($result_random <= 70){ echo "Display ad1"; } // if result less than or equal 90, display ad2 (20%) else if($result_random <= 90){ echo "Display ad2"; } // if result less than or equal 100, display ad3 (10%) else { echo "Display ad3"; } ?> Link to comment Share on other sites More sharing options...
PHPisis Posted April 21, 2017 Share Posted April 21, 2017 I'm no pro. But this should help.. <?php $result_random = rand(1, 100); if($result_random <= 70){ echo "Display ad1"; } else if($result_random > 70 and $result_random <= 90){ echo "Display ad2"; } else if($result_random > 90) { echo "Display ad3"; } ?> Link to comment Share on other sites More sharing options...
cyberRobot Posted April 21, 2017 Share Posted April 21, 2017 @PHPisis - The if/else structure provided by finderya doesn't need to be changed. The elseif, for example, will only be executed when $result_random is greater than 70. @finderya - To add images to your PHP code, you need to use the <img> tag. if($result_random <= 70){ echo '<img src="ad1.jpg">'; } Alternate solution: the convoluted if structure could be replaced with something like this: $ads = array('ad1.jpg', 'ad2.jpg', 'ad3.jpg'); shuffle($ads); echo '<img src="' . $ads[0] . '">'; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.