Show Random ads (random text+random title)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Show Random ads (random text+random title)

    I needed a script to display ads with random text and random link on my site.
    very simple to use
    PHP Code:
    function random_ads($no){


    $ad_text = array(
    "Text 1",
    "Text 2",
    "Text 3",
    "Text 4",
    "Text 5",
    "Text 6",
    "Text 7",
    );
    $ad_link = array(
    "http://mysite.com/1",
    "http://mysite.com/2",
    "http://mysite.com/3",
    "http://mysite.com/4",
    "http://mysite.com/5",
    "http://mysite.com/6",
    "http://mysite.com/7",
    "http://mysite.com/8",
    "http://mysite.com/9",
    "http://mysite.com/10",
    "http://mysite.com/11",


    );

    for(
    $y=1;$y<=$no;$y++){
    $rand_t array_rand($ad_text2);
    $rand_l array_rand($ad_link2);
    $text=$ad_text[$rand_t[1]];
    $link=$ad_link[$rand_l[1]];

    $random_ad.="Ad:<a href='$link'>$text</a><br />";
    }
    return 
    $random_ad;


    Usage
    Code:
    echo random_ads(2);  // 2 = number of ads displayed



    your updates are welcome
    Unamos los corazones,hoy todos somos multicolores!

    #2
    you should remove the last comma in array,
    anyway here is the same code just a bit different example.

    PHP Code:
    function random_ads($ads=1)
    {
        
    $ad_text = array(
        
    "Text 1",
        
    "Text 2",
        
    "Text 3",
        
    "Text 4",
        
    "Text 5",
        
    "Text 6",
        
    "Text 7"
        
    );
        
    $ad_link = array(
        
    "http://mysite.com/1",
        
    "http://mysite.com/2",
        
    "http://mysite.com/3",
        
    "http://mysite.com/4",
        
    "http://mysite.com/5",
        
    "http://mysite.com/6",
        
    "http://mysite.com/7",
        
    "http://mysite.com/8",
        
    "http://mysite.com/9",
        
    "http://mysite.com/10",
        
    "http://mysite.com/11"
        
    );
        for(
    $i=0;$i<$ads;$i++)
            
    $ad .= '<a href="'.$ad_link[rand(0count($ad_link)-1)].'">'.$ad_text[rand(0count($ad_text)-1)].'</a><br/>';
        return 
    $ad;
    }

    echo 
    random_ads(5); // to show 5 random ads 
    Advertise your mobile site for FREE with AdTwirl

    Comment


      #3
      Why remove the last comma? I've always wondered if it has an effect in the array or on performance. It doesn't count nor appear in a var dump.
      Any specific reasons?
      Perfection comes at a cost



      I accept liberty!

      Comment


        #4
        Originally posted by frostymarvelous View Post
        Why remove the last comma? I've always wondered if it has an effect in the array or on performance. It doesn't count nor appear in a var dump.
        Any specific reasons?
        i think in javascript it would cause an error or in mysql when you select rows, not in php, the correct way is when you remove the last comma
        Advertise your mobile site for FREE with AdTwirl

        Comment


          #5
          Best quotes random also can be used as ads

          quotes.php
          Code:
          <?php
          //////////////////////////////////////////////////////
          //
          // This is a VERY simple PHP Random Quote generator.
          //   I provide the source code for anyone who wants it for free.
          //   I also provide NO support for this program as well...
          //   Use it at your own risk ;-)
          //     If you use the script, please send me an email that says "Thanks"
          //     and what the URL is for your website. <quotes_nospam@davidmarotz.com>
          //
          //////////////////////////////////////////////////////
          
          $bgcolor = '#FFFFCC';
          $textcolor = 'black';
          $textsize = '2';
          
          // Array Structure:  "Quote","Author"
          
          $allqts = array
              ("The Black Knight Always Triumphs!", 
                  "Monty Python",
              "I swear by my life and love of it that I will never live for the sake of<br>another man, nor ask another man to live for mine" ,
                  "Atlas Shrugged",
              "It is clear that the individual who persecutes a man, his brother,<br> because he is not of the same opinion, is a monster",
                  "Voltaire",
              "I agree that there is a natural aristocracy among men<br>The grounds of this are virtue and talents.",
                  "Thomas Jefferson",
              "Liberty, when it begins to take root, is a plant of rapid growth.", 
                  "George Washington",
              "Never argue with an idiot. <br>They drag you down to their level <br>then beat you with experience",
                  "Dilbert",
              "The Answer is 42. What is the question?",
                  "Hitchikers Guide to the Galaxy",
              "Anyone who has never made a mistake has never tried anything new",
                  "Albert Einstein",
              "Progress doesn't come from early risers, progress is made<br>by lazy men looking for easier ways to do things.",
                  "Lazarus Long <font size=-2>(Time Enough for Love by Robert A. Heinlein)</font>",
              "Throughout history, poverty is the normal condition of man. <br>Advances which permit this norm to be exceeded - here and there, now and then - <br>are the work of an extremely small minority, frequently despised,<br> often condemned, and almost always opposed by all right-thinking people. <br>Whenever this tiny minority is kept from creating, or (as sometimes happens)<br> is driven out of a society, the people then slip back into abject poverty.<br><br>This is known as 'bad luck.'",
                  "Robert Heinlein",
              "A little learning is a dangerous thing; Drink deep, or taste not the<br>Pierian spring.  There shallow draughts intoxicate the brain, <br>and drinking largely sobers us again",
                  "Alexander Pope",
              "The early bird gets the worm, but the second mouse gets the cheese",
                  "Anonymous",
              "Subjugating the enemy's army without fighting is the true pinnacle of excellence",
                  "Sun-tzu, The Art of War",
              "Work as though you were to live 100 years; pray as if you were to die tomorrow",
                  "Benjamin Franklin",
              "The world is a stage, but the play is badly cast",
                  "Oscar Wilde",
              "Truth is generally the best vindication against slander.",
                  "Abraham Lincoln",
              "...mercy to the guilty is cruelty to the innocent...",
                  "Adam Smith",
              "...I wish that I may never think the smiles of the great and powerful<br> a sufficient inducement to turn aside from the straight path<br> of honesty and the convictions of my own mind",
                  "David Ricardo",
              "Democracy is the worst form of government except for all the others",
                  "Winston Churchill",
              "You can only know the highest peaks if you have experianced the lowest valley's",
                      "Richard Nixon",
              "They dress the wound of my people as though it were not serious. <br>'Peace, peace,' they say, when there is no peace.",
                  "Jeremiah 6:14",
              "It is better to remain silent and be thought a fool<br> than to open your mouth and remove all doubt.",
                  "Jonathan Swift",
              "The market system delivers the goods people want,<br>but those who make it work cannot readily explain why it is so.<br>The socialst or communist system does not deliver the goods, <br>but those who operate it can readily explain away its failure.",
                  "F.A. Hayek, Law, Legislation and Liberty, Vol. II",
              "Never Stop Exploring<sup>tm</sup>",
                  "The North Face"
                  );
          
          // Gets the Total number of Items in the array
          //  Divides by 2 because there is a Quote followed by an Author
          $totalqts = (count($allqts)/2);
          
          // Subtracted 1 from the total because '0' is not accounted for otherwise
          $nmbr = (rand(0,($totalqts-1)));
          $nmbr = $nmbr*2;
          
          //$nmbr = 18;
          
          $quote = $allqts[$nmbr];
                   $nmbr = $nmbr+1;
          $author = $allqts[$nmbr];
          
          // You can delete this section
          //   it is only so Search engines can find it
          if ($PHP_SELF == "/quotes.php") {
              echo "<Title>Random Quote Script for PHP</title>";
              echo "<meta name=\"Description\" content=\"Random Quote script written in PHP\">";
              echo "<meta name=\"keywords\" content=\"Random Quote script written in PHP\">";
              echo "This is my random quote script that is written in PHP";
              echo "<br>Feel free to use it for whatever you want<br/><br/>";
          }
          /// End Delete
          
          
          $space = "<font color=$bgcolor>.....................................</font>";
          $comments = "<br><center><font size='-2'><i><a href='quotes.php'>Random Quote Script by Dave</a></i></font></center>";
          
          echo "<center>";
          echo "<Font color=$textcolor size='$textsize'><i>";
          echo "$quote<br>";
          echo "</i></font>";
          echo "$space $author";
          echo "$comments";
          echo "</center>";
          
          IF ($_SERVER['PHP_SELF'] == "/quotes.php") {
              show_source("quotes.php");
             }
          
          ?>
          Visit: Chat4u.mobi - The New Lay Of being a site of your dreams!
          Visit: WapMasterz Coming Back Soon!
          _______
          SCRIPTS FOR SALE BY SUBZERO
          Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders.
          FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others...
          _______
          Info & Tips
          php.net
          w3schools.com

          Comment


            #6
            check dis
            PHP Code:
            //set the urls 
            $urls = array("http://abc.com" ,"http://def.com" ,"http://ghi.com" ,"http://jkl.com" ); 
             
            //set the text links 
            $text = array("link text1" ,"link text2" ,"link text3""link text4"); 
                    
            srand(time()); 
             
            //set the number in (rand()%3); for however many links there are 
                    
            $random = (rand()%3); 
            echo (
            "<a href = \"$urls[$random]\">$text[$random]</a>"); 

            http://WapTops.com- Get Real High Traffic
            http://WapFun.info-Fun 4 Wap

            Comment

            Working...
            X