在php的单个帖子中添加广告

时间:2018-03-02 07:26:11

标签: php

我有一个帖子页面,我希望广告会在php的帮助下出现在中心,但我说的是,我可以将单个帖子分成几个部分,第二个部分将继续广告类似于屏幕截图。

screenshot

如何? 我的代码是



  if(isset($_GET['title'])){
    $title = $_GET['title'];
    $query = "SELECT * FROM articles WHERE status='publish' and title=$title";

    $run = mysqli_query($con, $query);
    if(mysqli_num_rows($run) > 0){
      $row = mysqli_fetch_array($run);

      $id = $row['id'];
      $date = $row['date'];
  //	$day = $date['mday'];
  //	$month = $date['month'];
  //	$year = $date['year'];
      $image = $row['image'];
      $title = $row['title'];
      $author = $row['author'];
      $author_image = $row['author_image'];
      $article_data = $row['article_data'];

    }
    else{
      header('Location: index');
    }
  }

  /* Displaying in articel page */

      <b>By <?php echo ucfirst($author);?></b>
      <hr style="margin: 5px">
      <?php echo $date;?>
      </div>
      <div class="col-md-8">
        <h4><?php echo $title;?></h4>
        <hr>
        <img src="uploads/art_img/<?php echo $image;?>" class="expand" width="90%" alt="">
        <br><br>
        <p>
            <?php echo $article_data;?>
          </p>
         <a href="#">
         <div class="col-md-12 text-center"  style="height:auto;">
         <br>
        <h1>Adsense / Advertisement</h1>
         </div>
         </a>

        <p>
          <?php echo $article_data;?>
        </p>
      </div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

<强>更新

使用Jquery

<script type="text/javascript">
    var countP = $("#articleBody p").length;
if(countP > 1){
   $("#articleBody p:eq("+parseInt(countP/2)+")").after('<div>Рекламный блок!</div>');
}
</script>

您可以使用ob_start(); ob_get_contents(); ob_end_clean();插入广告

例如:

ob_start();
include "ads.php"; //file with you ads code
$ads = ob_get_contents();
ob_end_clean(); 
$aData = preg_replace('#{ads}#', $ads, $article_data);
echo $aData;

但是在文字中你必须有{ads}吗?你想去哪里看广告:

  

发布文字....发布文字..... {ads} posy txt .....发布文字....

var countP = $("#articleBody p").length;
if(countP > 1){
   $("#articleBody p:eq("+parseInt(countP/2)+")").after('<div>Рекламный блок!</div>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="articleBody">
  <h1>title</h1>
  <p>Post text .....</p>
  <p>Post text .....</p>
  <p>Post text .....</p>
  <p>Post text .....</p>
  <p>Post text .....</p>
  <p>Post text .....</p>
  <p>Post text .....</p>
</div>

答案 1 :(得分:0)

在文章文字中为您的广告部分添加标识符,请参阅{{ad}},然后将其替换为您的广告部分。

$article_data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.{{ad}} Nunc at purus molestie mi fringilla maximus. Sed eu est metus. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. ";

$article_data = str_replace('{{ad}}','<h1>Here ad</h1>',$article_data);

echo $article_data;