我一直在搜索以前的帖子,但还没有找到我要找的东西。可能是因为我不知道如何表达它或找到它的术语是什么。这个脚本所做的只是从数据库中抓取文章并将它们列出来。很简单,对。那里没有问题,但我想为广告插入一个 div,例如每列出 3-4 个条目。所以它会是 post#1、post#2、post#3、AD here、post#4、post#5、post#6、Another Ad Here,等等。有没有办法做到这一点,这需要进一步的搜索帮助吗?
$blogList = '';
include "connect.php";
$sql = mysql_query("SELECT * FROM articles WHERE id >8 LIMIT 6");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
// get all the product details
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$article_title = $row["article_title"];
$category = $row["category"];
$readmore = $row["readmore"];
$author = $row["author"];
$date_added = $row["date_added"];
$content = $row["content"];
$short = substr(strip_tags($content), 0, 750);
$shortTitle = substr(strip_tags($article_title), 0, 45);
$blogList .= ' <div class="blogSnippetTitle"><a href="http://www.#.com/better-trader.php?id='.$id.'"><h2>'.$article_title.'</h2></a></div><div class="blogSnippet"><div class="blogImage"><img src="http://www.#.com/trading_images/'.$id.'.jpg" height="142px" width="200px" alt="'.$category.' '.$shortTitle.'" /></div><div class="blogSnippetPrev"><div class="citation">By <span style="color:#006699;">'.$author.'</span> on <span style="color:#99aacc;">'.$date_added.'</span> in <span style="color:#006699;">'.$category.'</span></div>
<div class="snippet">'.$short.'...<br /></div>
<div class="readMoreButton"><br /><a href="http://www.#.com/better-trader.php?id='.$id.'"><img src="http://www.#.com/images/read_more.png" alt="read more graphic" /></a></div>
</div>
</div>
';
}
}else {
$blogList = "You have no products listed in your store yet";
}
mysql_close();
?>
Sergio 这是更新的代码。
<?php
// Check to see the URL variable is set and that it exists in the database
// Connect to the MySQL database
$blogList = '';
include "connect.php";
$sql = mysql_query("SELECT * FROM articles WHERE id >8 ORDER BY id DESC LIMIT 6");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
// get all the product details
$counter = 0;
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$article_title = $row["article_title"];
$category = $row["category"];
$readmore = $row["readmore"];
$author = $row["author"];
$date_added = $row["date_added"];
$content = $row["content"];
$short = substr(strip_tags($content), 0, 750);
$shortTitle = substr(strip_tags($article_title), 0, 45);
$counter++;
if ($counter == 4){ $blogList .= '<div class="blogSnippetTitle"><a href="http://www.#.com/better-trader.php?id='.$id.'"><h2>'.$article_title.'</h2></a></div><div class="blogSnippet"><div class="blogImage"><img src="http://www.#.com/trading_images/'.$id.'.jpg" height="142px" width="200px" alt="'.$category.' '.$shortTitle.'" /></div><div class="blogSnippetPrev"><div class="citation">By <span style="color:#006699;">'.$author.'</span> on <span style="color:#99aacc;">'.$date_added.'</span> in <span style="color:#006699;">'.$category.'</span></div>
<div class="snippet">'.$short.'...<br /></div>
<div class="readMoreButton"><br /><a href="http://www.#.com/better-trader.php?id='.$id.'"><img src="http://www.#.com/images/read_more.png" alt="read more graphic" /></a></div>
</div>
</div>' test; $counter = 0; }
}else {
$blogList = "You have no products listed in your store yet";
}
mysql_close();
?>
我不确定 AD 是什么意思 Sergio。如果这不是你的意思,请告诉我。