【发布时间】:2015-10-19 19:49:53
【问题描述】:
已解决:请查看我的回答,了解我是如何解决问题的。
我不确定这是否可行,因为我对数组了解不多,但这里什么都没有。
我想在我的网站上添加广告。
我想我可以找到一种方法来实现多维数组来控制内容。
我想出了这个:
$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>';
在$ads 上执行var_dump 并得到以下结果:
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
【问题讨论】:
标签: php arrays object multidimensional-array rotator