PHP模数包装div并忽略LIMIT

时间:2014-06-09 00:12:06

标签: php modulus

我正在尝试每2次迭代添加一个容器。我已经实现了一个计数,但是当我检查页面时,我得到了奇怪的结果。

任何想法可能会发生什么以及我在包装注射时可能出错了什么?

<?php
include "includes2014_new/db_conx.php";
$sql ="SELECT * FROM articles WHERE id ORDER BY id DESC LIMIT 5";
$resultsHome = mysqli_query($db_conx,$sql);
$productCount = mysqli_num_rows($resultsHome); // count the output amount
if ($productCount > 0) {

$counter = 1;

while($row = mysqli_fetch_array($resultsHome)){
$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, 80);

     if (($counter % 2) === 1) {
         $blogList .= '<div class="clearer">';
         }
         //----------------
            if($readmore == ''){
                    $blogList .= '<div class="col span_6_of_12 postBox textLeft fulltwelvepadfix">
                    <div class="section miniPad">
                    <div class="col span_8_of_12">'.$article_title.'</div>
                    <div class="col span_4_of_12"><img src="images/'.$id.'.jpg" height="80px" width="100px" alt="'.$category.' " /></div>
                    </div>
                    <div class="section miniPad">
                    <div class="col span_12_of_12">'.$date_added.' - '.$author.'</div>
                    <div class="col span_12_of_12"><p>'.$short.'[...]</p></div>
                    <div class="section group"><a href="r.php?id='.$id.'">Read More</a></div>
                    </div>
                    <div class="comments section group">
                    <div class="commentsInner">Comments(0)</div>
                    </div>
                    </div>';
                }

            else {
                $blogList .= '<div class="col span_6_of_12 postBox textLeft fulltwelvepadfix">
            <div class="section miniPad">
            <div class="col span_8_of_12">'.$article_title.'</div>
            <div class="col span_4_of_12"><img src="images/'.$id.'.jpg" height="80px" width="100px" alt="'.$category.' " /></div>
            </div>
            <div class="section miniPad">
            <div class="col span_12_of_12">'.$date_added.' - '.$author.'</div>
            <div class="col span_12_of_12"><p>'.$short.'[...]</p></div>
            <div class="section group"><a href="'.$readmore.'">Read More</a></div>
            </div>
            <div class="comments section group">
            <div class="commentsInner">Comments(0)</div>
            </div>
            </div>';
            }
        //----------------
    if (($counter % 2) === 0) { $blogList .= '</div>'; }
    $counter++;
    }
}
?>

1 个答案:

答案 0 :(得分:0)

尝试更改

SELECT * FROM articles WHERE id ORDER BY id DESC LIMIT 5

SELECT * FROM articles WHERE `id`= '$id' ORDER BY id DESC LIMIT 5