已解决:请参阅我的答案以了解我如何解决我的问题。
我不确定这是否可能,因为我对数组了解不多,但这里什么都没有。
我想在我的网站上添加广告。
我想我可以找到一种方法来实现多维数组来控制内容。
我想出了这个:
$ads = array(
"ad1" => array(
title => "Advertisement Title",
url => "http://example.com",
image => "http://example.com/images/example.jpg",
description => "Advertisement Description"),
"ad2" => array(
title => "Advertisement Title",
url => "http://example.com",
image => "http://example.com/images/example.jpg",
description => "Advertisement Description"),
"ad3" => array(
title => "Advertisement Title",
url => "http://example.com",
image => "http://example.com/images/example.jpg",
description => "Advertisement Description")
);
我通过语法检查器运行了这段代码,没有错误,所以我认为我至少在正确的轨道上。
我不明白的是如何编写一个foreach
随机选择其中一个广告的循环。
我需要更改"ad1" => array(
为ad[1] => array(
吗?
我没有使用太多数组,所以我不知道如何在 for each 循环中定位它的特定部分。
我希望提出一个 foreach 循环,它会输出如下内容:
<a href="UrlFromArray"><img src="ImageSrcFromArray" alt="TitleFromArray">
<br>
<p>DescriptionFromArray</p>
这是可以实现的吗?
编辑和更新:
function displayAds728x90() {
$ads = array(
"ad1" => array(
'title' => "Advertisement Title",
'url' => "http://example.com",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description"),
"ad2" => array(
'title' => "Advertisement Title",
'url' => "http://example.com",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description"),
"ad3" => array(
'title' => "Advertisement Title",
'url' => "http://example.com",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description")
);
$randomAd = array_rand($ads);
echo '<a href="'.$randomAd->url.'" target="_blank">';
echo '<img src="'.$randomAd->image.'" alt="'.$randomAd->title.'">';
echo '</a>';
echo '<p>';
echo $randomAd->description;
echo '</p>';
}
displayAds728x90();
按照 Dynelight 给出的答案,我想出了上面的代码。
现在我唯一的问题是我收到以下错误:
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 23
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 27
只是为了让您知道我的完整代码是哪些行号:
<img src="http://www.example.com/images/your_banner_here.png">
<?php
function displayAds728x90() {
$ads = array(
"ad1" => array(
'title' => "Advertisement Title",
'url' => "http://example.com",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description"),
"ad2" => array(
'title' => "Advertisement Title",
'url' => "http://example.com",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description"),
"ad3" => array(
'title' => "Advertisement Title",
'url' => "http://example.com",
'image' => "http://example.com/images/example.jpg",
'description' => "Advertisement Description")
);
$randomAd = array_rand($ads);
echo '<a href="'.$randomAd->url.'" target="_blank">';
echo '<img src="'.$randomAd->image.'" alt="'.$randomAd->title.'">';
echo '</a>';
echo '<p>';
echo $randomAd->description;
echo '</p>';
}
displayAds728x90();
?>
关于是什么导致这些错误的任何想法?
更新 2:
编辑了以下部分并添加了缺失的代码位:
$randomAd = array_rand($ads);
echo '<a href="'.$ads->$randomAd->url.'" target="_blank">';
echo '<img src="'.$ads->$randomAd->image.'" alt="'.$ads->$randomAd->title.'">';
echo '</a>';
echo '<p>';
echo $ads->$randomAd->description;
echo '</p>';
执行一次var_dump
并$ads
得到以下结果:
array(3) { ["ad1"]=> array(4) { ["title"]=> string(19) "Advertisement Title" ["url"]=> string(18) "http://example.com" ["image"]=> string(37) "http://example.com/images/example.jpg" ["description"]=> string(25) "Advertisement Description" } ["ad2"]=> array(4) { ["title"]=> string(19) "Advertisement Title" ["url"]=> string(18) "http://example.com" ["image"]=> string(37) "http://example.com/images/example.jpg" ["description"]=> string(25) "Advertisement Description" } ["ad3"]=> array(4) { ["title"]=> string(19) "Advertisement Title" ["url"]=> string(18) "http://example.com" ["image"]=> string(37) "http://example.com/images/example.jpg" ["description"]=> string(25) "Advertisement Description" } }
从上面发布的完整页面代码开始,错误现在是:
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 23
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 23
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 27
Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 27