使用 PHP 将 MySQL 中的内容插入段落
Insert content from MySQL to paragraph using PHP
我正在尝试使用 PHP 在我的 $article_content 中插入广告,我需要根据段落编号放置广告代码。
当我对 MySQL 执行 select 时,我得到了这个结构:
ID_ARTICLE |广告代码 |段落
可能大多数文章都会有 04 行数据(04 条广告),例如:
1 | adcode/adcode | 0
1 | adcode/adcode | 1
1 | adcode/adcode | 3
1 | adcode/adcode | 5
所以这是我的代码:
$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>';
$doc = new DOMDocument();
$doc->loadHTML($article_content);
while($row3 = $stmt3->fetch()) {
for ($i = 0; $i < $row3['paragraph']; $i++) {
$p = $doc->getElementsByTagName('p');
if ($i == $row3['paragraph']) {
$ads = $doc->createElement('div', $row3['adcode']);
$p->insertBefore($ads);
}
}
}
echo $doc->saveHTML();
我不知道如何正确创建 for
这是我所说的意思的一个例子:
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);
foreach ($adverts as $row3) {
$paragraphs = $doc->getElementsByTagName('p');
for ($i = $paragraphs->length; --$i >= 0; ) {
$paragraph = $paragraphs->item($i);
if ($i + 1 == $row3['paragraph']) {
$ads = $doc->createElement('div', $row3['adcode']);
$paragraph->parentNode->insertBefore($ads, $paragraph);
}
}
}
echo $doc->saveHTML();
请注意段落项目 运行 从 0 到 n-1,并且由于您的段落编号 运行s 从 1 到 n,因此需要更正。
我正在尝试使用 PHP 在我的 $article_content 中插入广告,我需要根据段落编号放置广告代码。
当我对 MySQL 执行 select 时,我得到了这个结构:
ID_ARTICLE |广告代码 |段落
可能大多数文章都会有 04 行数据(04 条广告),例如:
1 | adcode/adcode | 0
1 | adcode/adcode | 1
1 | adcode/adcode | 3
1 | adcode/adcode | 5
所以这是我的代码:
$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>';
$doc = new DOMDocument();
$doc->loadHTML($article_content);
while($row3 = $stmt3->fetch()) {
for ($i = 0; $i < $row3['paragraph']; $i++) {
$p = $doc->getElementsByTagName('p');
if ($i == $row3['paragraph']) {
$ads = $doc->createElement('div', $row3['adcode']);
$p->insertBefore($ads);
}
}
}
echo $doc->saveHTML();
我不知道如何正确创建 for
这是我所说的意思的一个例子:
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);
foreach ($adverts as $row3) {
$paragraphs = $doc->getElementsByTagName('p');
for ($i = $paragraphs->length; --$i >= 0; ) {
$paragraph = $paragraphs->item($i);
if ($i + 1 == $row3['paragraph']) {
$ads = $doc->createElement('div', $row3['adcode']);
$paragraph->parentNode->insertBefore($ads, $paragraph);
}
}
}
echo $doc->saveHTML();
请注意段落项目 运行 从 0 到 n-1,并且由于您的段落编号 运行s 从 1 到 n,因此需要更正。