PHP随机生成广告图片的实例教程Php

印迹发布于:2019-2-24 1078

随机生成广告代码1:
<?php/** PART 1 - THE ARRAY OF ARRAYS* I have expanded this to make the script easier to negotiate* If you're using constant values, you can remove those parts.* If wanted, you could also add captions to the arrays below.*/
$ad_rand = array(
1 => array('url' => 'somesite1.com', // Url of advertiser or stats recording redirect
'img' => 'image1.jpg', // Add file name, type, even path if needed
'ttl' => 'Tool Tip 1', // Recommended title text (tool tip)
'alt' => 'Alt Text 1' // Required alternate text
),
2 => array('url' => 'somesite2.com',
'img' => 'image2.jpg',
'ttl' => 'Tool Tip 2',
'alt' => 'Alt Text 2'
),
3 => array('url' => 'somesite3.com',
'img' => 'image3.jpg',
'ttl' => 'Tool Tip 3',
'alt' => 'Alt Text 3'
),
4 => array('url' => 'somesite4.com',
'img' => 'image4.jpg',
'ttl' => 'Tool Tip 4',
'alt' => 'Alt Text 4'
),
5 => array('url' => 'somesite5.com',
'img' => 'image5.jpg',
'ttl' => 'Tool Tip 5',
'alt' => 'Alt Text 5'
),
);
?>

输出:

<?php/** PART 2 - THE OUTPUT* This is simple, simply place the link/image line where needed.* I included the path as a fixed part in this example* I added an optional container div. Image string is XHTML.* Note that the ?> of the last part and <?php of this part* can be removed if you decide to combine the two sections.* Also note that contstant variables like path (shown) and* height and width dimensions can be added below and removed* from the section above if that better meets your needs*/
$ad = array_rand($ad_rand);
echo('<div><a href="http://'.$ad_rand[$ad]['url'].'/" title="'.$ad_rand[$ad]['ttl'].'"><img src="http://www.virplus.com/imgs/'.$ad_rand[$ad]['img'].'" width="468" height="60" alt="'.$ad_rand[$ad]['alt'].'" /></a></div>');?>


法二:
<?php
    $ad = array(
        "广告代码_1",
        "广告代码_2",
        "广告代码_3",
    );    //广告代码随机数组
    $rand = array_rand($ad);    //获取随机数组的键
    echo $ad[$rand];    //输出随机数组的值,也就是随机广告
?>
相关导航:

1. PHP随机显示图片的代码(刷新变换图片) 

2. php自动读取文件夹下所有图片    

3. php遍历、读取文件夹中图片并分页显示图片的方法

4. 利用PHP随机显示指定文件夹下某一图片的方法

5. 随机显示文件夹下图片文件的方法



http://www.virplus.com/thread-133.htm
转载请注明:2019-2-24 于 VirPlus 发表

推荐阅读
最新回复 (0)

    ( 登录 ) 后,可以发表评论!

    返回