April 5, 2012

While loop returning data

Question by relentless

In the code below, I’m pulling all upcoming training classes from my database. I’m checking to see if the endDate has passed and I’m also checking if the status !='2'

I want this to return the 4 most recent results. It works fine until statusdoes =2. I understand that the loop is technically running 4 times, but only displaying the results with status !='2'

How can I change this so that if status = '2' the loop will continue until it finds 4 results that meet the criteria?

<?php
$today = date("Y-m-d");
$count = 0;
$sth = $dbh->query('SELECT * from training ORDER BY startDate ASC');  
        $sth->setFetchMode(PDO::FETCH_ASSOC); 
            while($count <= 4 && $row = $sth->fetch()) { 
                if($row['endDate'] > $today && $row['status'] != '2') {?>
                    <li>
                    <img class="post_thumb" src="/images/img.jpg" alt="" />
                    <div class="post_description">
                        <small class="details">
                            <?php echo date("m/d/Y", strtotime($row['startDate'])) . ' - ' . date("m/d/Y", strtotime($row['endDate'])) ?>
                        </small>
                        <a class="post_caption" href="/register.php?course_id=<?php echo $row['courseId'] . '&id=' . $row['id'] ?>"><?php echo $row['title'] ?></a>
                    </div>
                    </li>
                <?php }
                    $count++;
                    }
                ?>  

Answer by Victor Nitu

You must put the $count++ inside the if loop, otherwise it will always get incremented.
As in:

<?php
$today = date("Y-m-d");
$count = 0;
$sth = $dbh->query('SELECT * from training ORDER BY startDate ASC');  
        $sth->setFetchMode(PDO::FETCH_ASSOC); 
            while($count <= 4 && $row = $sth->fetch()) { 
                if($row['endDate'] > $today && $row['status'] != '2') {?>
                    <li>
                    <img class="post_thumb" src="/images/img.jpg" alt="" />
                    <div class="post_description">
                        <small class="details">
                            <?php echo date("m/d/Y", strtotime($row['startDate'])) . ' - ' . date("m/d/Y", strtotime($row['endDate'])) ?>
                        </small>
                        <a class="post_caption" href="/register.php?course_id=<?php echo $row['courseId'] . '&id=' . $row['id'] ?>"><?php echo $row['title'] ?></a>
                    </div>
                    </li>
                <?php 
                $count++;
                }
            }
?>

Answer by Starx

You can break out out of the loop, if its four

while($row = $sth->fetch()) { 
        ....
        if($row['status']=='2' && $count >="4") break;
        $count++;
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!