-3

次のコードがあります。

<?php

    mysql_connect("localhost","root","");
    mysql_select_db("bravo");
    $res=mysql_query("select * from coisas");

?>
<div>
<?php
    while ($row=mysql_fetch_array($res)) {

    echo "<img src=\"{$row['imagem']}\">";

    }
?>
</div>

そして、画像間のランダムな位置にアドセンス コードを表示する必要があります。

4

2 に答える 2

2

その間にいくつかのコードを追加します。

while ($row=mysql_fetch_array($res)) {
   echo "<img src=\"{$row['imagem']}\">";
   echo "--- adsense code here---";
}

すべての画像の後にそれを挿入します。

または、完全にランダムな位置に配置したい場合:

$chanceOfAdsense = 35; // 35% chance of adsense appearing between any given images

while ($row=mysql_fetch_array($res)) {
   echo "<img src=\"{$row['imagem']}\">";

   if (mt_rand(0,99) < $chanceOfAdsense) {
      echo "--- adsense code here---";
   }
}
于 2013-09-23T18:34:52.480 に答える
0

while ループから出力配列を作成し、その中の値の数を数えます。count(array) を使用して rand() で乱数を計算し、インデックスが乱数と一致する場合の if ステートメントを使用して、出力配列を反復処理します。

<?php

    mysql_connect("localhost","root","");
    mysql_select_db("bravo");
    $res=mysql_query("select * from coisas");

?>
<div>
<?php
    $output_array = array();
    while ($row=mysql_fetch_array($res)) {

      $output_array[] = "<img src=\"{$row['imagem']}\">";

    }

    //If you want the image to appear closer to middle, use fractions of $output_array
    //EG: rand(count($output_array)/3, count($output_array)*2/3));
    $rand_key = rand(0, count($output_array)-1);

    foreach ($output_array as $key => $img) {
       if ($key == $rand_key) {
           //echo your adsense code
       }
       echo $img;
    }
?>
</div>
于 2013-09-23T18:34:08.370 に答える