Thread: Adrotator
View Single Post
Old Feb 1st, 2008, 8:10 AM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Adrotator

You could wrap the code I linked to, into a function, and save it as a separate PHP script.

adrotater.php

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. function adrotater($filename) {
  4. $fcontents = join ('', file ($filename));
  5. $s_con = split("~",$fcontents);
  6.  
  7. $banner_no = rand(0,(count($s_con)-1));
  8. echo $s_con[$banner_no];
  9. }
  10.  
  11. ?>

Place the above file, adrotater.php, in the same directory as your other scripts. This would allow you to use the function, in your main PHP file, like so:

index.php

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. // Include the separate PHP script
  4. include('adrotater.php');
  5.  
  6. //
  7. // Blah blah blah, some code here
  8. //
  9.  
  10. // Display a random advertisement from banner_ads.txt
  11. adrotater('banner_ads.txt');
  12.  
  13. //
  14. // More code here
  15. //
  16.  
  17. ?>

Make sure to include the adrotater.php file in every file you want to use the adrotater function.

Then the argument passed to it, 'banner_ads.txt', is the text file with the advertisements inside.
Sane is offline   Reply With Quote