[SOLVED] Keep statement out of a loop
Moderator: General Moderators
[SOLVED] Keep statement out of a loop
Hi,
How do you create a $sql statment that checks a column if there are any 1's show an if statment ..
I can do the if statement bla bla, but how do I keep it out of a loop .. so if there is more than one 1 display the if statement once .. rather than in a loop where it will display the if statement for every match ..
Thanks
How do you create a $sql statment that checks a column if there are any 1's show an if statment ..
I can do the if statement bla bla, but how do I keep it out of a loop .. so if there is more than one 1 display the if statement once .. rather than in a loop where it will display the if statement for every match ..
Thanks
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
$did_it = false;
while($row = mysql_fetch_assoc($query))
{
if(!$did_it && $rowї'column'] == '1')
{
$did_it = true;
echo 'I did it!';
}
}- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Hi,
The code above displays nothing even tho there are three 1's in the column ..
Basically if there is a 1 = new message which is switched to a 0 once read .. If the row new has any 1's in it i want it to show an image .. but only show it once no matter how many 1's are in the column .. if there are no 1's then show nothin ..
Thanks
Code: Select all
$sql="SELECT new FROM pm WHERE user_name = '".$_SESSIONї'user_name']."'";
$new = false;
while($row = mysql_fetch_assoc($sql))
{
if(!$new && $rowї'new'] == '1')
{
$new = true;
echo '<img src="images/new.gif" border="0">';
}Basically if there is a 1 = new message which is switched to a 0 once read .. If the row new has any 1's in it i want it to show an image .. but only show it once no matter how many 1's are in the column .. if there are no 1's then show nothin ..
Thanks
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: