2016-05-04 97 views
-5

您好我想在我的產品頁面中顯示隨機廣告,我的所有產品調用while循環顯示在主頁中,我想隨機在這些產品之間添加廣告在同樣的設計中,像產品展示一樣在freekaamaal中如何在我的網站上顯示隨機廣告

我如何在產品之間展示這些廣告你能否給我這個想法?

see this for more info

<div class="products-grids"> 
<div class="col-md-12"> 
    <?php 

     $id=$_GET['id']; 

     $result1 = mysqli_query($con,"select * from products1 where sta='Active' order by id DESC LIMIT 0 , $resultsPerPage"); 
     while($ro = mysqli_fetch_array($result1)) 
     { 
      $nam1=substr($ro['pne'],0,60); 
      $url1=$ro['url']; 
      $description1=substr($ro['description'],0,200); 
      $price1=$ro['price']; 
      $price11=$ro['price1']; 
      $bid1=$ro['company']; 
      $image=$ro['image_name']; 
      $time=$ro['time']; 
      $tag=$ro['tag']; 
    ?> 
    <div class="col-md-3"> 
     <div class="hentry post1 id="post-225396"> 

     Display Product in loop 

     </div> 
    </div> 
    <?php } mysqli_close($con);?> 
<!-- <div class="col-md-3"> 
     <div class="hentry post1 id="post-225396"><img src="ads.jpg"></div> 
    </div> --> 
</div> 
<div class="clearfix"> &nbsp;</div> 

+0

你還沒有明白如何使用堆棧溢出:[我如何問一個好問題?](http://stackoverflow.com/help/how-to-ask) –

+0

你只使用PHP或cms like wordpress或其他什麼 –

+0

其核心PHP @nirpendra –

回答

0

反正你的問題太寬泛。

這是一步一步的解決方案。

FRONTEND HTML + PHP

//兩個硬DIV'S +三個隨機廣告,但只有一個AD GONNA觸發。

<?php $random_number = rand(0,2); ?> 

<?php ads($random_number,0); ?> 
<div>your image or whatever</div> 
<?php ads($random_number,1); ?> 
<div>your image or whatever</div> 
<?php ads($random_number,2); ?> 

<?php $random_number = rand(0,2); ?> 

<?php ads($random_number,0); ?> 
<div>your image or whatever</div> 
<?php ads($random_number,1); ?> 
<div>your image or whatever</div> 
<?php ads($random_number,2); ?> 

比較函數

//現在創建一個函數來比較,如果產生的隨機數等於廣告的佔位符。

function ads($rand_num, $placeholder){ 
    if($rand_num == $placeholder){ 
     echo "<div> YOUR ADS CONTENT </div>"; 

    } 
} 

注:這並不意味着完美運行,它只是在如何實現這一具體目標綱要。

+0

我使用while循環從數據庫中提取產品,並且我希望以相同的順序在這些產品之間顯示廣告或任何內容,以便更好地理解這一點,您可以看到freekaamaal.com這是基於WordPress和在這裏,他們展示隨機廣告代替產品的地方相同,我在我的核心PHP希望你得到它,順便說一句,感謝很多理解:) –

+0

@anupdwivedi在你的問題和你添加你的循環和代碼在那裏將試圖幫助你這樣 –

+0

chk out親愛的代碼添加在@Nirpendra Patel –

0

做這樣的事(我想編寫代碼的時候很容易理解):所以有可能有語法錯誤的機會

<?php 

function show_an_ad() 
{ 
    $the_ad = ''; 

    switch (mt_rand(0,2)) 
    { 
     case 0: 
      $the_ad = 'ad0.jpg'; 
     break; 
     case 1: 
      $the_ad = 'ad1.jpg'; 
     break; 
     case 2: 
      $the_ad = 'ad2.jpg'; 
     break; 
    } 
    echo '<div class="col-md-3"><div class="hentry post1"><img src="'.$the_ad.'"></div></div>'; 
} 


$ads_every_how_many_products = 3; 


<div class="products-grids"> 
    <div class="col-md-12"> 
    <?php 

     $id=$_GET['id']; 

     $result1 = mysqli_query($con,"select * from products1 where sta='Active' order by id DESC LIMIT 0 , $resultsPerPage"); 

     $counter = 1; 
     while($ro = mysqli_fetch_array($result1)) 
     { 
      $nam1=substr($ro['pne'],0,60); 
      $url1=$ro['url']; 
      $description1=substr($ro['description'],0,200); 
      $price1=$ro['price']; 
      $price11=$ro['price1']; 
      $bid1=$ro['company']; 
      $image=$ro['image_name']; 
      $time=$ro['time']; 
      $tag=$ro['tag']; 
    ?> 
     <div class="col-md-3"> 
      <div class="hentry post1" id="post-225396"> 

      Display Product in loop 

      </div> 
     </div> 
    <?php 
      if(!($counter%$ads_every_how_many_products)) 
      { 
       show_an_ad(); 
      } 

      $counter++; 
     } 
     mysqli_close($con); 
    ?> 
    </div> 
</div> 
<div class="clearfix"> &nbsp;</div> 

我沒有一個PHP系統在這裏或東西

相關問題