Jump to content


Photo

Simple Ad Rotation Script


  • Please log in to reply
2 replies to this topic

#1 finderya

finderya
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 21 April 2017 - 12:32 PM

http://www.phpeasyst...m/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"; 


?>



#2 PHPisis

PHPisis
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 21 April 2017 - 12:46 PM

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"; 
} 

?>


#3 cyberRobot

cyberRobot
  • Moderators
  • Much to Learn
  • 2,719 posts

Posted 21 April 2017 - 03:11 PM

@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] . '">';

 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users