Home > database >  Insert content from MySQL to paragraph using PHP
Insert content from MySQL to paragraph using PHP

Time:04-24

I am trying to insert ads to my $article_content using PHP, I need to place the ad code according the paragraph number.

When I do a select to my MySQL I got this structure:

ID_ARTICLE | AD CODE | PARAGRAPH

Probably the most of articles gonna have 04 rows of data (04 ads), something like:

1 | adcode/adcode | 0
1 | adcode/adcode | 1
1 | adcode/adcode | 3
1 | adcode/adcode | 5

So this is my code:

$pdo = ConectarSite();

$sql3 = "
SELECT
   art.id,
   ab.adcode,
   ap.paragraph 
FROM
   artigos art 
   LEFT JOIN
      anuncios_artigo aa 
      ON art.id = aa.id_artigo 
   LEFT JOIN
      anuncios_bloco ab 
      ON aa.id_anuncios_bloco = ab.id 
   LEFT JOIN
      anuncios_posicao ap 
      ON aa.id_anuncios_posicao = ap.id 
WHERE
   art.id = :id
";
$stmt3 = $pdo->prepare($sql3);
$stmt3->execute(['id' => '1']);


$article_content = '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been</p>';

while($row3 = $stmt3->fetch()) {


        $doc = new DOMDocument();
        $doc->loadHTML($article_content);
        $i = 0;

        foreach ($doc->getElementsByTagName('p') as $p) {
            $i  ;

            if ($i == 1) {
                $ads = $doc->createElement('div', $row3['adcode']);
                $p->insertBefore($ads);
            }
        }

        echo $doc->saveHTML();

}

Output:

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been</p>
<div>ADS</div>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been</p>
<div>ADS</div>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been</p>

I don't know where to place the paragraph result to make it works correctly, and echo the output without duplicating the content, I got the DOM code from web and trying to adapt to my logic...

CodePudding user response:

Here's an example of what I meant when I said:

You should create, fill and output your DOMDocument outside the main while ()` loop, only the inserting of the ads should be inside the loop.

$doc = new DOMDocument();
$doc->loadHTML($article_content);

while($row3 = $stmt3->fetch()) {
    $i = 0;
    foreach ($doc->getElementsByTagName('p') as $p) {
        $i  ;
        if ($i == 1) {
            $ads = $doc->createElement('div', $row3['adcode']);
            $p->insertBefore($ads);
        }
    }
}

echo $doc->saveHTML();

I didn't really test the code, so I cannot guarantee there are no other problems. For instance the ($i == 1) doesn't seem right, considering what you want to achieve.

  • Related