user9097004
user9097004

Reputation:

add advertisement inside in a single post in php

I have a post page where I want advertisement will appear in center by the help of php, but what I'm saying, is it possible that i can divide single post into to parts and the second part will continue after ads similar to screenshot.

screenshot

How? my code is

  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>

Upvotes: 2

Views: 556

Answers (2)

Illia Yaremchuk
Illia Yaremchuk

Reputation: 2025

UPDATE

with Jquery

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

You mau use ob_start(); ob_get_contents(); ob_end_clean(); for insert ads

For example:

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;

But in text you must have {ads} in place? where you want see advertise:

Post text .... post text ..... {ads} posy txt ..... post text....

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>

Upvotes: 1

Andrei Todorut
Andrei Todorut

Reputation: 4526

Add an identifier for you ad section in the article text see {{ad}} and then replace this with your ad section.

$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;

Upvotes: 0

Related Questions