An if statement inside a while statement

I’m trying to make a price comparison site. I would like to check if the record has been added/updated today.

Problem is, the if statement inside the while loop is echoing ‘Hello World’ for every record. In fact, I have only two records with todays date on. I once read somewhere that you cannot put an if statement in a while loop which I find hard to believe.


<?php
$qid = mysql_query("SELECT * FROM compare as p LEFT JOIN comparetosection as s ON p.prod_code = s.product_id WHERE section_url='$url' ORDER BY `price` ASC");
while ($row = mysql_fetch_array($qid)){
echo '<div class="rowHold">';
echo '<div class="store">'.$row['store'].'';
$date = mysql_query("SELECT dateTime FROM compare WHERE dateTime >= CURDATE()"); 
if ($testdate = mysql_fetch_array($date)){
echo '<div style="float: right; margin: 0px 10px 0px 0px;">Hello World</div>';
}
echo '</div>';
echo '<div class="price">£'.$row['price'].'</div>';
echo '<div class="visitSite"><a href="'.$row['storeurl'].'"><img src="../images/visit_this_site.jpg" width="120" height="18" alt="'.$row['store'].'" border="0"/></a></div>';
echo '</div>';
}
?>

Problem is, the if statement inside the while loop is echoing ‘Hello World’ for every record.

that’s because the test condition in the IF statement is evaluating to true every single time.

have a look at comparison operators and what [fphp]mysql_query[/fphp] returns.

Hi there, I’ve taken a peek at Comparison Operators and think I may be getting somewhere. Not displaying anyrthing though now. Any ideas as to what I’m doing wrong dude?

<?php
$qid = mysql_query("SELECT * FROM compare as p LEFT JOIN comparetosection as s ON p.prod_code = s.product_id WHERE section_url='$url' ORDER BY `price` ASC");
$date = mysql_query("SELECT dateTime FROM compare"); 
$today = date("Y-m-d");
while ($row = mysql_fetch_array($qid)){
echo '<div class="rowHold">';
echo '<div class="store">'.$row['store'].'';
if ($date==$today){
echo '<div style="float: right; margin: 0px 10px 0px 0px;">Hello World :)</div>';
}
echo '</div>';
echo '<div class="price">£'.$row['price'].'</div>';
echo '<div class="visitSite"><a href="'.$row['storeurl'].'"><img src="../images/visit_this_site.jpg" width="120" height="18" alt="'.$row['store'].'" border="0"/></a></div>';
echo '</div>';
}
?>

if you read through the other link I posted, your error should become clear.

this coding and debugging work flow might also help.

Perfect, worked out the errors of my way. It’s all working perfectly now. Thank you again, really appreciated. Life saver!

<?php
$qid = mysql_query("SELECT * FROM compare as p LEFT JOIN comparetosection as s ON p.prod_code = s.product_id WHERE section_url='$url' ORDER BY `price` ASC");
$today = date("Y-m-d");
while ($row = mysql_fetch_array($qid)){
echo '<div class="rowHold">';
echo '<div class="store">'.$row['store'].'';
if ($row['dateTime']==$today){
echo '<div style="float: right; margin: 0px 10px 0px 0px;">Hello World :)</div>';
}
echo '</div>';
echo '<div class="price">£'.$row['price'].'</div>';
echo '<div class="visitSite"><a href="'.$row['storeurl'].'"><img src="../images/visit_this_site.jpg" width="120" height="18" alt="'.$row['store'].'" border="0"/></a></div>';
echo '</div>';
}
?>