• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

While statement, how to end at last loop or strip off final piece?

I have a while statement that adds an 'OR' after each loop.

My final result needs to have no 'OR' at the end.  Do I need to do the while statement differently or just strip off the final 'OR' somehow after the while statement is done?

The script:

---------------------
$result1 = mysql_query("SELECT * FROM Accts WHERE em = '".$em."'");

while ($row=@mysql_fetch_array($result1))
{
$savedID=$row["savedID"];

$savedids = $savedids." id='".$savedID."' OR";
}
--------------------

$savedids currently results in:  id='551' OR id='588' OR  

but needs to be:  id='551' OR id='588'  //no 'OR' at the end

Thanks for any help you can provide,   Chris


0
St_Aug_Beach_Bum
Asked:
St_Aug_Beach_Bum
  • 2
  • 2
  • 2
  • +2
1 Solution
 
morristhebearCommented:
The best way would be to strip it off the end after the loop, adding a conditional inside the loop to check would be inefficient as it would need to evaluate every time.

Try adding this after the  loop:

$savedids = substr ($savedids, 0, strlen($savedids) - 2);
0
 
NeoificCommented:
$savedids = substr($savedids, 0, -3);
Add that outside of your while loop.  It'll strip the last 3 characters off of your string, which are " OR"
0
 
hieloCommented:
you need to remove it afterwards
$result1 = mysql_query("SELECT * FROM Accts WHERE em = '".$em."'"); 
while ($row=@mysql_fetch_array($result1))
{
$savedID=$row["savedID"]; 
$savedids = $savedids." id='".$savedID."' OR";  
} 
$savedids = substr($savedids  , 0  , strlen($savedids)-3 );

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
fosiul01Commented:
Hi yah

here you are fetching data from table to variable by using while loop, is not it ??

i am not clear about your this statement :
$savedids = $savedids." id='".$savedID."' OR";

here it should be,
$savedids = $row["savedids"];


why you ddnot write like this ??




0
 
NeoificCommented:
It's less code and less time to use a negative length rather than subtracting the length from the calculated string length and passing that.
0
 
St_Aug_Beach_BumAuthor Commented:
Wow, thank you all, I am testing.....
0
 
morristhebearCommented:
I agree Neoific.  Mine was just a quick solution, I didn't realise you could use a negative number until I checked documentation just now.
0
 
St_Aug_Beach_BumAuthor Commented:
Thanks again everyone.

I will go with Neoific's solution.  morristhebear's worked as well and was a little earlier, but I do like the shorter code and morristhebear said he agrees.

Chris
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now