1

is it possible to run only one query using php & mysql and then do two while loops with different results. i

//this is the query
$qry_offers = mysql_query("SELECT * FROM offers ORDER BY offer_end ASC");

in the first loop, i want to show any result which has an "end_date" less or equal than today

<div id="current">
<?
while($row_offers = mysql_fetch_assoc($qry_offers)) {
    if ( (date("Y-m-d H:i:s")) <= ($row_offers['offer_end']) ) {
        echo '<li><a href="#">'.$row_offers['offer_name'].'</a></li>';
    }
}
?>
</div><!-- END OF CURRENT -->

in the second loop, i want to show any result which has an "end_date" greater than today

//this is where i would have the 2n while loop
<div id="inactive">
<?
   while($row_offers = mysql_fetch_assoc($qry_offers)) {
    if ( (date("Y-m-d H:i:s")) > ($row_offers['offer_end']) ) {
        echo '<li><a href="#">'.$row_offers['offer_name'].'</a></li>';
    }
}
?>
</div><!-- END OF INACTIVE -->
3
  • have you tried it? what is not working about this? Commented Sep 5, 2012 at 1:59
  • @bretterer: yes, i tried it and i only get results for the 1st loop. the second is blank Commented Sep 5, 2012 at 2:04
  • use mysql_field_seek($qry_offers,0) before second loop but I prefer Ansrew's answer! Commented Sep 5, 2012 at 2:16

2 Answers 2

5

The solution is to save the results from each loop and then put them together at the end.

$offers_current = array();
$offers_inactive = array();

while($row_offers = mysql_fetch_assoc($qry_offers)) {
    if ( (date("Y-m-d H:i:s")) <= ($row_offers['offer_end']) )
        $offers_current[] = '<li><a href="#">'.$row_offers['offer_name'].'</a></li>';
    if ( (date("Y-m-d H:i:s")) > ($row_offers['offer_end']) )
        $offers_inactive[] = '<li><a href="#">'.$row_offers['offer_name'].'</a></li>';
}

?>
<div id="current">
    <ul>
    <?php echo implode("\n", $offers_current) ?>
    </ul>
</div>
<div id="inactive">
    <ul>
    <?php echo implode("\n", $offers_inactive) ?>
    </ul>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for a single pass through the dataset. Could also replace the second if with an else now that they are right next to each other.
@Tony Not using else provides a slightly more generic solution, though the truth is I didn't see it. One of the most common mistakes I see in PHP is passing around huge data structures willy-nilly, like a resultset. It is just so much more scalable to process a resultset as you read it and I want to encourage that.
4

You could do this:

$itemsArray = mysql_fetch_assoc($qry_offers);
foreach ( $itemsArray as $row_offers ) {
    // ... Code in here for less than 'offer_end'
}
foreach ( $itemsArray as $row_offers ) {
    // ... Code in here for greater than 'offer_end'
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.