Page 1 of 1
[SOLVED] Keep statement out of a loop
Posted: Sat Feb 19, 2005 2:40 pm
by Jim_Bo
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
Posted: Sat Feb 19, 2005 2:43 pm
by feyd
use a flag style variable that you check against as well. When you run the if the first time, flip the flag such that the if will not evaluate to true again.
Posted: Sat Feb 19, 2005 2:50 pm
by Jim_Bo
Hi,
Can you put me on to an example .. I havnt heard of what you are saying before .. a quick google search didnt really help me out at all
Thanks
Posted: Sat Feb 19, 2005 2:52 pm
by feyd
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!';
}
}
Posted: Sat Feb 19, 2005 2:53 pm
by smpdawg
Can you be a little more specific? What are you trying to accomplish? Do you have test data that we can see? Do you want it to be just a SQL statement or a mix of SQL and PHP?
Posted: Sat Feb 19, 2005 3:14 pm
by John Cartwright
His example is self explanatory. It will loop, and only loop if the following creteria are met
$did_it == false
$row['column'] == 1
So once those criteria are met, he then changes the "flag" ($did_it) to false therefor, the if statement will only ever happen once.
Posted: Sat Feb 19, 2005 3:19 pm
by Jim_Bo
Hi,
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">';
}
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
Posted: Sat Feb 19, 2005 3:22 pm
by John Cartwright
Can you show us your database structure, why would you have multiple 1's in the column??
Posted: Sat Feb 19, 2005 3:31 pm
by Jim_Bo
Hi,
when a message is sent a 1 is added to the "new" column in the message row .. when the message is read .. it is updated to a 0
If there is one or more 1 down the column .. show image but only once .. If there are no 1's down the column display nothing
Thanks
Posted: Sat Feb 19, 2005 3:43 pm
by feyd
your code isn't performing any query.

Posted: Sat Feb 19, 2005 9:19 pm
by Jim_Bo
Hi,
..

forget I asked ..
Thanks guys .. got it all working great ..