0

I have set-up a news page which retrieves news from MYSQL news table.

I am trying to identify if the news column is odd or even, so if the news columns is odd or even it will add a class to the div element.

My code is as follows:

<?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>";
}
?>
pavel
  • 26,538
  • 10
  • 45
  • 61
Craig
  • 185
  • 1
  • 12

5 Answers5

3

TAke any flag which maintains odd-even position...

$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!";

}
Shaunak Shukla
  • 2,347
  • 2
  • 18
  • 29
1

Create variable, increment that in each loop and check, if $i % 2 is 0 (even) or 1 (odd).

$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++;
} 
kero
  • 10,647
  • 5
  • 41
  • 51
pavel
  • 26,538
  • 10
  • 45
  • 61
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!";

 }

Use counter variable and check if it is even or odd ?

Shailesh Katarmal
  • 2,757
  • 1
  • 12
  • 15
0

Take $classname = '';

Get news ID in loop and divide / 2 and compare whether you getting odd / even value. In case odd value add $classname = 'oddCLASS' and in case even value add $classname = 'evenCLASS' and updare class = $classname wherever you required.

This way it will automatically update dynamic class.

CodeWithCoffee
  • 1,896
  • 2
  • 14
  • 36
0

so the fastest way to do this is just by using a boolean:

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

No need to keep a counter for that, no need for a modulo and it keeps the code pretty clean. My preferred way unless you need to keep a counter. And even then: $counter & 1 == 1 is faster than $counter % 2 == 1 and does exactly the same.

Simple explanation:

echo ($odd = !$odd) ? 'odd' : 'even';
// will become
$odd = ! $odd; // it flips the boolean
if ($odd)
  echo 'odd';
else
  echo 'even';
DoXicK
  • 4,784
  • 24
  • 22