0

我一直在搜索以前的帖子,但还没有找到我要找的东西。可能是因为我不知道如何表达它或找到它的术语是什么。这个脚本所做的只是从数据库中抓取文章并将它们列出来。很简单,对。那里没有问题,但我想为广告插入一个 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。如果这不是你的意思,请告诉我。

4

2 回答 2

2

您可以在循环中添加一个计数器。例如:

$counter = 0; // before the loop starts

并在循环内:

$counter++;
if ($counter == 4){ $blogList .= "/* your AD here*/" ; $counter = 0; }

希望这会有所帮助(请记住您的 mysql_query 已弃用,请在此处
阅读更多信息)

编辑:

    <?php 
    // Check to see the URL variable is set and that it exists in the database
    // Connect to the MySQL database  

    error_reporting(E_ALL ^ E_NOTICE);  //this is for debugging, remove if you dont need anymore
    ini_set("display_errors", 1);       //this is for debugging, remove if you dont need anymore

    $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($productCount)){ 
            $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>'; 
    $counter = 0;}
$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();
    ?>
于 2013-06-23T19:37:21.830 回答
1

使用模运算符(称为 % 或 MOD)。

模产生整数除法的余数。所以 3 MOD 3 = 0、5 MOD 2 = 1、10 MOD 3 = 1 等等(参见http://en.wikipedia.org/wiki/Modulo_operation)。

在前面的答案中使用循环变量

$loopcount = 0;

while(...) {
  if ($loopcount MOD 3 == 0) {
    // do this every 3rd loop iteration
  }
  $loopcount++;
}

这样您就不必重置循环计数器。

于 2013-06-24T06:52:18.860 回答