如果页面上只有2行,则添加中断

时间:2017-11-29 07:00:02

标签: php mysql

如果PAGE上只有2行(不是每2行)(不是在mysql中),我需要添加像</div>这样的换行符来结束。

我的代码:

$results_per_page = 12;
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * $results_per_page;
$sth = $db->prepare("SELECT * FROM users WHERE active='1' ORDER BY vip DESC, date DESC LIMIT $start_from, ".$results_per_page);
$sth->execute();
while($answer=$sth->fetch()){ if (only 2 rows on PAGE) { blabla } }

3 个答案:

答案 0 :(得分:0)

只需使用

$sth = $db->prepare("SELECT * FROM users WHERE active='1' ORDER BY vip DESC, date DESC LIMIT $start_from, ".$results_per_page);
$sth->execute();

// better to get result in variable
$result = $sth->fetchAll();

if (count($result) == 2) {
   echo '</div>';
}

// to display rows
foreach($result as $row) {
    // $row is array, it contains your columns
    echo $row['content'];
}

答案 1 :(得分:0)

$i = 0;
while($answer=$sth->fetch()){ 
    if ($i<2) {
       // do stuff...

    }
    else { echo '</div>';break; }
    $i++;
}

此代码在回显并退出循环后运行 $ i = 0 $ i = 1

答案 2 :(得分:0)

$i=0;
while($answer=$sth->fetch()){ 
   //do stuff
   $i++;
}
if($i==2){
echo "<div>Whatever You Wants.</div>";
}

如果结果中只有2行,它将回显