如何识别它是奇数还是偶数并添加类?

时间:2015-02-11 10:56:39

标签: php mysql

我已经设置了一个新闻页面,可以从MYSQL新闻表中检索新闻。

我正在尝试确定新闻列是奇数还是偶数,所以如果新闻列是奇数或者甚至它会在div元素中添加一个类。

我的代码如下:

<?php
$cat = $_GET['cat'];
$date = $_GET['date'];

if ($date !="") {
    $date = explode('-', $date);
    $year = $date[1];
    $month = $date[0];
    $month = date("m", strtotime($month));
    $sql = "SELECT * FROM news WHERE year(newsDate) = '$year' AND month(newsDate) = '$month' AND newsState = 1 ORDER BY newsDate DESC";
} else {
    $sql = "SELECT * FROM news WHERE newsState = 1 ORDER BY newsDate DESC";
}

$result = $conn->query($sql);
$rows = $result->num_rows;

$pager = new PS_Pagination($conn, $sql, 5, 10, null);

$rs = $pager->paginate();

$num = $rs->num_rows;

if($num >= 1 ){  
while($row = $rs->fetch_assoc()){
?>
 <div class="news <?php echo $num; ?>">
        <div class="four columns">
            <p><img src="/news-images/thumbs/thumb_<?php echo $row['newsImage1']; ?>" alt=""/></p>
        </div>
        <div class="eight columns">
            <h3><?php echo $row['newsTitle']; ?></h3>
            <p><?php echo stripslashes(strip_tags($row['newsDescription'])); ?></p>
        </div>
    </div>
<?php } } else {
echo "No records found!";

}

if ($rows >= 5) {
echo "<div class='page-nav blog-nav'>";
    echo $pager->renderFullNav();
echo "</div>";
}
?>

5 个答案:

答案 0 :(得分:2)

任何保持奇偶位置的旗帜......

$f = 0; //ADDED THIS LINE
if($num >= 1 ){  
while($row = $rs->fetch_assoc()){
 if($f%2==0) //ADDED THIS LINE
    $class_name = "even"; //ADDED THIS LINE
 else     //ADDED THIS LINE
    $class_name = "odd";  //ADDED THIS LINE
?>
 <div class="news <?php echo $class_name; ?>">
        <div class="four columns">
            <p><img src="/news-images/thumbs/thumb_<?php echo $row['newsImage1']; ?>" alt=""/></p>
        </div>
        <div class="eight columns">
            <h3><?php echo $row['newsTitle']; ?></h3>
            <p><?php echo stripslashes(strip_tags($row['newsDescription'])); ?></p>
        </div>
    </div>
<?php $f++; } } else {
echo "No records found!";

}

答案 1 :(得分:1)

创建变量,在每个循环中递增,并检查$i % 2是0(偶数)还是1(奇数)。

$i = 1;

while($row = $rs->fetch_assoc()){
?>
 <div class="news <?php echo $num; echo $i % 2 == 0 ? ' even' : ' odd' ?>">
        <div class="four columns">
            <p><img src="/news-images/thumbs/thumb_<?php echo $row['newsImage1']; ?>" alt=""/></p>
        </div>
        <div class="eight columns">
            <h3><?php echo $row['newsTitle']; ?></h3>
            <p><?php echo stripslashes(strip_tags($row['newsDescription'])); ?></p>
        </div>
    </div>
<?php 
    $i++;
} 

答案 2 :(得分:1)

if($num >= 1 ){  
$tr = 1;
while($row = $rs->fetch_assoc()){
 if($tr%2 == 0)
 {
     //then even class
 }
 else
 {
    //odd class
 }
  ?>
 <div class="news <?php echo $num; ?>">
    <div class="four columns">
        <p><img src="/news-images/thumbs/thumb_<?php echo  $row['newsImage1']; ?>" alt=""/></p>
    </div>
    <div class="eight columns">
        <h3><?php echo $row['newsTitle']; ?></h3>
        <p><?php echo stripslashes(strip_tags($row['newsDescription']));   ?></p>
    </div>
 </div>
<?php $tr++; } } else {
echo "No records found!";

 }

使用计数器变量并检查它是偶数还是奇数?

答案 3 :(得分:0)

取$ classname ='';

获取循环中的新闻ID并除以/ 2并比较您是否获得奇数/偶数值。如果奇数值添加$ classname ='oddCLASS',万一甚至值添加$ classname ='evenCLASS'并更新class = $ classname,无论你需要什么。

这样它会自动更新动态类。

答案 4 :(得分:0)

所以最快的方法就是使用布尔值:

$odd = false;
while ( .... )
{
    echo ($odd = !$odd) ? 'odd' : 'even';
}

无需为此保留计数器,不需要模数,它可以保持代码非常干净。除非你需要保留一个柜台,否则我首选的方式。即便如此:$counter & 1 == 1$counter % 2 == 1快,并且完全相同。

简单说明:

echo ($odd = !$odd) ? 'odd' : 'even';
// will become
$odd = ! $odd; // it flips the boolean
if ($odd)
  echo 'odd';
else
  echo 'even';