Implementation Code of php random sorting Advertisement

Source: Internet
Author: User

The advertising personnel are very concerned about where the ads will be placed, because this may affect the number of clicks, or even whether they are displayed on the first screen. this problem can be easily solved, as long as the advertisement is displayed randomly.

How to Implement the code? Here I recommend two solutions for randomly displaying advertisements.

Processing at the backend
On the back-end sorting page, store ad nodes in arrays, randomly sort the arrays, and then output the sorted arrays. The reference code (PHP) is as follows:
Copy codeThe Code is as follows:
// Store the ad list with Arrays
$ Ads = array ('<a href = "#"> </a>'
, '<A href = "#"> </a>'
, '<A href = "#"> </a>'
, '<A href = "#"> </a>'
);

// Random sorting of Arrays
Shuffle ($ ads );

// Output the sorted Array
$ Html = '';
Foreach ($ ads as $ ad ){
$ Html. = $ ad;
}
Echo $ html;

Let's expand. If I am a webmaster, I have reserved four ad spaces, but now only three are serving. I want to put a "Waiting" ad rent link in a vacant ad space, and displayed at the end. What should I do? After sorting, insert the ad leasing link.
Copy codeThe Code is as follows:
// Store the ad list with Arrays
$ Ads = array ('<a href = "#"> </a>'
, '<A href = "#"> </a>'
, '<A href = "#"> </a>'
);

// Random sorting of Arrays
Shuffle ($ ads );

// Output the sorted Array
$ Html = '';
Foreach ($ ads as $ ad ){
$ Html. = $ ad;
}

// Add an ad leasing Link
$ Html. = '<a href = "#"> </a> ';
Echo $ html;

I use this method to output 125x125 ads, because it is intuitive and reliable and easy to handle. However, if you want to make static pages, we recommend that you use the JS random sorting method.

Processing at the front end
In the back-end, output in the original order and re-order the page by using JavaScript. Assume that the HTML segment of the page output advertisement area is as follows.
Copy codeThe Code is as follows:
<Div id = "ads">
<A href = "#"> </a>
<A href = "#"> </a>
<A href = "#"> </a>
<A href = "#"> </a>
</Div>

We can use JS to re-Sort advertisements. The reference code is as follows:
Copy codeThe Code is as follows:
<Div id = "ads" style = "display: none;">
<A href = "#"> </a>
<A href = "#"> </a>
<A href = "#"> </a>
<A href = "#"> </a>
</Div>
<Div id = "random-ads" style = "display: none;">
</Div>

<Script type = "text/javascript">
// <! [CDATA [

Var source = document. getElementById ('analytics ');
Var target = document. getElementById ('random-ads ');
Var ads = source. getElementsByTagName ('A ');

// Subscript Array
Var arr = new Array ();
For (var I = 0; I <ads. length; I ++ ){
Arr [I] = I;
}

// Random sorting
Function randomSort (a, B ){
Var tmp = parseInt (Math. random () + 0.5), 10 );
Return tmp? A-B: B-;
}

// Randomly insert the nodes in the old advertising area into the new advertising Area
Arr. sort (randomSort );
For (var I = 0; I <arr. length; I ++ ){
Target. appendChild (ads [arr [I]. cloneNode (true ));
}

// Display new ad areas and remove old ones
Source. parentNode. removeChild (source );
Target. style. display = 'block ';

//]>
</Script>

What should I do if I want to scale up like method 1 to display the empty ad space at the end and display the ad leasing link? This is an exercise after class...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.