MYSQL三项然后div跟随三项然后Div

时间:2013-06-23 19:29:07

标签: php mysql

我一直在搜索以前的帖子,但还没能找到我想要的东西。可能是因为我不知道如何表达它或者找到它的术语是什么。这个脚本的作用就是从数据库中抓取文章并列出来。很简单,对。没有问题,但我想插入广告div,例如列出的每3-4个条目。因此,它将发布#1,发布#2,发布#3,AD发布,发布#4,发布#5,发布#6,另发布广告等。有没有办法做到这一点,这将被称为进一步搜索帮助?       

        $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来说意味着什么。如果这不是您的意思,请告诉我。

2 个答案:

答案 0 :(得分:2)

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

$counter = 0; // before the loop starts

并在循环内部:

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

希望这会有所帮助 (并记住您的mysql_query已弃用,请详细了解 here

编辑:

    <?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();
    ?>

答案 1 :(得分:1)

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

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

在previois答案中使用循环变量

$loopcount = 0;

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

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