我正在创建一篇文章wesbite,并希望显示一篇包含8篇文章的网格,其中广告使用PHP放置在该网格中。所以它应该看起来像(例如):
[ Article ][ Article ][ Article ]
[ Article ][ Article ][ ADVERTS ]
[ Article ][ Article ][ Article ]
所以我的PHP代码是:
$get_article_sql = "SELECT * FROM articles ORDER BY added DESC LIMIT 8";
$get_article_res = mysqli_query($con, $get_article_sql);
while($article = mysqli_fetch_assoc($get_article_res)){
$article_id = $article["id"];
etc...
$list_articles .= "
<div class=\"selectArticle selectArticleClear\">
<a href=\"article.php?id=$article_id&title=$article_url_title\">
<div class=\"selectArticleImage\">
<img class=\"selectArticleImage\" src=\"images/$article_year/$article_id$article_photo\" alt=\"$article_alt\" />
<div class=\"selectArticleCover\"></div>
</div>
</a>
<div class=\"selectArticleTitle\"><a href=\"article.php?id=$article_id&title=$article_url_title\">$article_title</a></div>
<div class=\"selectArticleText\">$article_article...<br>| ... <a href=\"article.php?id=$article_id&title=$article_url_title\">Read On</a> »»</div>
</div>
";
}
然后我有我的广告:
$advert = "<div class="advert">ADVERT CODE</div>";
如何将$advert
置于$list_articles
字符串中的第6位?
答案 0 :(得分:3)
计数6输出并连接您的广告div:
...
$i = 1;
while($article = mysqli_fetch_assoc($get_article_res)){
if(6 === $i++) {
$list_articles .= '<div class="advert">ADVERT CODE</div>';
}
...
答案 1 :(得分:0)
在你的
上 <div class=\"selectArticleTitle\"><a href=\"article.php?id=$article_id&title=$article_url_title\">$article_title</a></div>
将另一个div放在后面,以便更好地进行div组织,然后使用css来定位它
为
做好准备[ Article ][ Article ][ Article ]
[ Article ][ Article ][ ADVERTS ]
[ Article ][ Article ][ Article ]
使用CSS
答案 2 :(得分:-1)
您可以使用这样的计数器:
$i=0;
$advert = "<div class="advert">ADVERT CODE</div>";
while($article = mysqli_fetch_assoc($get_article_res)){
$article_id = $article["id"];
if($i==5){
$list_articles .=$advert;
}
$list_articles .= "
<div class=\"selectArticle selectArticleClear\">
<a href=\"article.php?id=$article_id&title=$article_url_title\">
<div class=\"selectArticleImage\">
<img class=\"selectArticleImage\" src=\"images/$article_year/$article_id$article_photo\" alt=\"$article_alt\" />
<div class=\"selectArticleCover\"></div>
</div>
</a>
<div class=\"selectArticleTitle\"><a href=\"article.php?id=$article_id&title=$article_url_title\">$article_title</a></div>
<div class=\"selectArticleText\">$article_article...<br>| ... <a href=\"article.php?id=$article_id&title=$article_url_title\">Read On</a> »»</div>
</div>
";
$i++;
}