3

解決済み: 問題をどのように修正したかについては、私の回答を参照してください。

配列についてあまり知らないので、これが可能かどうかはわかりませんが、ここでは何も起こりません。

Web サイトに広告を追加したいと考えています。

これに多次元配列を実装してコンテンツを制御する方法を見つけることができると考えました。

私はこれを思いつきました:

$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私が理解していないのは、広告の 1 つをランダムに選択するループの書き方です。

に変更"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_dumponを実行し$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
4

2 に答える 2

3

foreach 関数は、配列のすべての要素を反復処理するために使用されます。配列のランダムな要素を取得したい。この関数を調べてください。おそらく役に立つでしょう:

http://php.net/manual/en/function.array-rand.php

要素をランダムに取得し、次のように参照します。

<?php $random_element = array_rand ( $ads); ?>

<a href="<?php echo $ads->$random_element->url ?>">
<img src="<?php echo $ads->$random_element->image ?>" alt="<?php echo $ads->$random_element->title ?>"></a>
<p><?php echo $ads->$random_element->description; ?></p>
于 2015-07-29T02:19:17.583 に答える