[SOLVED] Keep statement out of a loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

[SOLVED] Keep statement out of a loop

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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!';
  }
}
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post 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">'; 
  &#125;
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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Can you show us your database structure, why would you have multiple 1's in the column??
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your code isn't performing any query. ;)
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,


.. :oops: forget I asked ..

Thanks guys .. got it all working great ..
Post Reply