Home Backend Development PHP Tutorial php 随机排序广告的实现代码_php技巧

php 随机排序广告的实现代码_php技巧

May 17, 2016 am 09:19 AM
advertise random order

投放广告的人都很关注他的广告会放在哪个位置, 因为这可能影响点击次数, 甚至是否在第一屏显示. 就这个问题, 其实很容易解决, 只要随机显示广告即可.

代码如何实现? 在这我推荐两种随机显示广告的处理办法.

在后端处理
在后端排序好再输出页面. 将广告节点用数组存放, 对数组进行随机排序, 再输出经过排序的数组. 参考代码 (PHP) 如下:

复制代码 代码如下:

// 用数组存放广告列表
$ads = array('广告 1'
,'广告 2'
,'广告 3'
,'广告 4'
);

// 对数组进行随机排序
shuffle($ads);

// 输出经过排序的数组
$html = '';
foreach ($ads as $ad) {
$html .= $ad;
}
echo $html;

我们来扩展一下, 如果我是站长, 预留了 4 个广告位, 但现在只有 3 个在投放; 我想在空置的广告位放置一个 "虚位以待" 的广告招租链接, 并显示在最后, 该如何处理呢? 在排序完成之后再插放广告招租链接就可以了.
复制代码 代码如下:

// 用数组存放广告列表
$ads = array('广告 1'
,'广告 2'
,'广告 3'
);

// 对数组进行随机排序
shuffle($ads);

// 输出经过排序的数组
$html = '';
foreach ($ads as $ad) {
$html .= $ad;
}

// 添加广告招租链接
$html .= '虚位以待';
echo $html;

我就是用这个方法来输出 125x125 广告的, 因为它直观可靠, 处理方便. 但是如果你希望对页面做静态化, 建议选用 JS 随机排序的方法.

在前端处理
在后端按原有循序输出, 在页面通过 JavaScript 重新排序. 假设页面输出广告区域的 HTML 片段如下.
复制代码 代码如下:


广告 1
广告 2
广告 3
广告 4


我们可以通过 JS 来对广告进行重新排序. 参考代码如下:
复制代码 代码如下:






如果有如同方法 1 那样的扩展需求, 将空广告位显示在最后, 且显示广告招租链接, 该如何处理? 这个当作课后习题吧...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to turn off the ads recommended by 360 Browser? How to turn off ads recommended by 360 Browser on PC? How to turn off the ads recommended by 360 Browser? How to turn off ads recommended by 360 Browser on PC? Mar 14, 2024 am 09:16 AM

How to turn off the ads recommended by 360 Browser? I believe that many users are using 360 Browser, but this browser sometimes pops up advertisements, which makes many users very distressed. Let this site carefully introduce to users how to Turn off the ads recommended by 360 Browser on your computer. How to turn off the ads recommended by 360 Browser on your computer? Method 1: 1. Open 360 Safe Browser. 2. Find the "three horizontal bars" logo in the upper right corner and click [Settings]. 3. Find [Lab] in the taskbar on the left side of the pop-up interface, and check [Enable "360 Hotspot Information" function]. Method 2: 1. First double-click

How to completely turn off ads in Moments How to completely turn off ads in Moments Mar 03, 2024 pm 12:31 PM

Users will encounter a lot of advertisements when browsing Moments, so how can they completely turn off the advertisements? Users need to click Settings in My, then find Personal Information and Permissions, then click Personalized Ads Management, and just turn off ad personalization. This article will tell you how to completely turn off ads in Moments, so take a look! WeChat usage tutorial: How to completely turn off ads in Moments? Answer: It cannot be turned off completely. You can only turn off personalized recommendations. Specific methods: 1. First, click on me, and then find the settings inside. 2. Find the personal information and permissions options. 3. Click on the personalized advertising management function inside. 4. Click the switch on the right side of ad personalization and turn it off. How to turn off ads: 1. Find the ads in Moments. 2. Click right

How to turn off ads in the new version of Edge? How to block Edge personalized ads How to turn off ads in the new version of Edge? How to block Edge personalized ads Mar 14, 2024 am 11:37 AM

After updating the Edge browser, many users found that there were a lot of advertisements on the interface. For example, at the bottom of the new tab page, there were links to multiple websites and the word advertisements were marked, which looked very annoying. Is there any way to turn off personalized ads in Edge browser? The editor has searched many methods on the Internet, and I will share with you a little trick to turn off ads. How to turn off ads in the new version of Edge? 1. Open the Edge browser. 2. Click [•••] in the upper right corner. 3. Click [Settings]. 4. Click [Privacy, Search and Services]. 5. Turn off the switch on the right side of "Personalize your web experience" to turn off the personalization pushed by Microsoft.

How to stop pop-ups on Windows 11 How to stop pop-ups on Windows 11 May 29, 2023 am 11:24 AM

Pop-ups and interruptions can be annoying when you're working or trying to focus on a piece of content. While mobile devices only have a DND toggle switch, what about Windows? Can you stop pop-ups on Windows 11? Let’s find out! Can you stop pop-ups on Windows 11? Yes, not at all. You can get rid of most pop-ups in Windows 11, however, some confirmation pop-ups and security notifications cannot be bypassed. Furthermore, there is no reliable way to get rid of all pop-ups with a single click. You need to close individual types of pop-ups to eliminate them on Windows 11. Luckily, we've compiled a list of what you'll need

How to turn off smart advertising recommendations on Douyin How to turn off smart advertising recommendations on Douyin Mar 22, 2024 pm 04:06 PM

When we usually use Douyin for entertainment, we often have some inexplicable and strange advertisements that disturb our leisure experience. Many users are not disturbed by them and want to turn off the recommendation push of these advertisements, but they don't. If you know how to operate it, then the editor of this website will bring you this tutorial guide and this detailed introduction, hoping to help everyone in need. First, we first find the option to understand and manage ad push in the app settings, then see all the ad-related switch options below, click the switch at the back to turn it off, and then a confirmation prompt to turn off the ad will pop up, we click to confirm. Close ads

Proofing tools missing in Microsoft Word [Resolved] Proofing tools missing in Microsoft Word [Resolved] Apr 18, 2023 pm 11:46 PM

Does “Proofing Tool Missing” appear in Microsoft Word when you open the application for editing? This message appears when Word cannot detect the proofing tools in Word. Usually, corruption in Word data files causes this problem. So, if you're wondering how to fix this problem, you've just landed at the right place. Fix 1 – Download Tool Download the specified proofing tool in Word directly from the error prompt. 1. Exit word (if you have opened it). 2. Now, open Word again. 3. Wait until it loads on your computer. When Word loads completely, click "Download" that appears on the ribbon. This will be done from the Office server in the background

How to close computer game advertising pop-ups How to close computer game advertising pop-ups Jul 07, 2023 pm 08:57 PM

Many netizens believe that they will encounter some advertisement pop-ups when playing on Windows 10 computers. Sometimes when playing games, game advertisement pop-ups may appear, resulting in a poor experience. So what should I do if the game advertisement flashes in the lower right corner of my win10 computer? How to close computer game advertising pop-ups? The following editor will teach you how to close game advertisement pop-ups on your computer. How to close computer game advertising pop-ups. Step 1: Find the process name of the flashing pop-up window. 1. When the flashing pop-up window appears, do not close it yet. Right-click to open the taskbar settings at the bottom of the computer. 2. Find the notification area under the taskbar and click Show selected icons on the taskbar. 3. The icons listed here once appeared on your taskbar, so even if you close the pop-up window, it is still recorded here. you

How to turn off ads that always pop up in Kuaishou Express Edition? How to turn off ad push in Kuaishou Express Edition? How to turn off ads that always pop up in Kuaishou Express Edition? How to turn off ad push in Kuaishou Express Edition? Apr 01, 2024 pm 09:34 PM

Kuaishou Express Edition has always been so popular with everyone. It can view a large number of videos every day. It is constantly updated, so that everyone can see these wonderful video contents anytime, anywhere. It is really rich, diverse and direct. To relieve worries and boredom, sometimes everyone always encounters some advertising pop-ups, which directly affects everyone's perception of watching videos, so everyone wants to close these advertising pop-ups to obtain a more comfortable viewing experience. Friends who don’t know how to operate it can take a look at the tutorial I brought to you. How to turn off advertising push in Kuaishou Express Edition: 1. First open the Kuaishou Express Edition software, go to the Kuaishou page and click the [three horizontal lines] icon in the upper left corner; 2. Then click [Settings] on the current page; 3. Enter

See all articles