我如何加入这两个字符串并根据条目列表创建随机链接。
// Add a link and the associated image //
$adlink1="http://www.****.com/sale.php";
$adlinkpic1="http://www.***-cdn.com/blogAssets/ad/1.jpg";
$adlink2="http://www.*****.com/sale.php";
$adlinkpic2="http://www.**-cdn.com/blogAssets/ad/2.jpg";
$adlink3="http://www.**.com/product.php?prodref=564_white&ref=AddSphere";
$adlinkpic3="http://www.**-cdn.com/blogAssets/ad/3.jpg";
$adlink4="http://www.**.com/wedding-boutique.php";
$adlinkpic4="http://www.**-cdn.com/blogAssets/ad/4.jpg";
$adlink5="http://www.**.com/made-to-measure-service.php";
$adlinkpic5="http://www.**-cdn.com/blogAssets/ad/5.jpg";
// SHOW ONE AD LINK
srand ((double) microtime() * 1000000);
$adlink[] + $adlinkpic[] = rand(0,count($quotes)-1);
echo "<a href='$adlink'><img src='$adlinkpic' />";
// SHOW TWO AD LINKS /cannot be same
// code here
答案 0 :(得分:3)
很容易将所有链接放在(关联)数组中,然后使用array functions来操作它们:
<?php
$ad = array(
array(
"url" => "http://www.****.com/sale.",
"img" => "http://www.***-cdn.com/blogAssets/ad/1.jpg"
),
array(
"url" => "http://www.*****.com/sale.",
"img" => "http://www.**-cdn.com/blogAssets/ad/2.jpg"
),
array(
"url" => "http://www.**.com/product.php",
"img" => "http://www.**-cdn.com/blogAssets/ad/3.jpg"
),
array(
"url" => "http://www.**.com/wedding-boutique.",
"img" => "http://www.**-cdn.com/blogAssets/ad/4.jpg"
),
array(
"url" => "http://www.**.com/made-to-measure-service.",
"img" => "http://www.**-cdn.com/blogAssets/ad/5.jpg"
)
// more ads
);
$id = array_rand($ad); // choose a random index from the array
echo "<a href=\"{$ad[$id]['url']}\"><img src=\"{$ad[$id]['img']}\" /></a>\n";
unset($ad[$id]); // remove the chosen one so that it is not displayed on next pass
$id = array_rand($ad);
echo "<a href=\"{$ad[$id]['url']}\"><img src=\"{$ad[$id]['img']}\" /></a>\n";
unset($ad[$id]);
答案 1 :(得分:1)
我很想做到这样的事情:
<?php
// Add a link and the associated image //
$adlink1="http://www.****.com/sale.php";
$adlinkpic1="http://www.***-cdn.com/blogAssets/ad/1.jpg";
$adlink2="http://www.*****.com/sale.php";
$adlinkpic2="http://www.**-cdn.com/blogAssets/ad/2.jpg";
$adlink3="http://www.**.com/product.php?prodref=564_white&ref=AddSphere";
$adlinkpic3="http://www.**-cdn.com/blogAssets/ad/3.jpg";
$adlink4="http://www.**.com/wedding-boutique.php";
$adlinkpic4="http://www.**-cdn.com/blogAssets/ad/4.jpg";
$adlink5="http://www.**.com/made-to-measure-service.php";
$adlinkpic5="http://www.**-cdn.com/blogAssets/ad/5.jpg";
$links = array();
$links[0]=array('link'=>$adlink1,'pic'=>$adlinkpic1);
$links[1]=array('link'=>$adlink2,'pic'=>$adlinkpic2);
$links[2]=array('link'=>$adlink3,'pic'=>$adlinkpic3);
$links[3]=array('link'=>$adlink4,'pic'=>$adlinkpic4);
$links[4]=array('link'=>$adlink5,'pic'=>$adlinkpic5);
$alreadyAdded=array();
for ($i=0;$i<2;$i++) {
$added = false;
while (!$added) {
// generate random number
$rand = mt_rand(0,4);
if (!in_array($rand,$alreadyAdded)) {
echo "<a href='".$links[$rand]['link']."'><img src='".$links[$rand]['pic']."' />";
$added = true;
$alreadyAdded[]=$rand;
}
}
}
编辑:注意到您想要输出超过1个更新的代码以反映。