Solved

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

Posted on 2008-06-24
8
194 Views
Last Modified: 2008-06-24
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
Comment
Question by:St_Aug_Beach_Bum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 1

Expert Comment

by:morristhebear
ID: 21856606
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
 
LVL 1

Accepted Solution

by:
Neoific earned 500 total points
ID: 21856613
$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
 
LVL 82

Expert Comment

by:hielo
ID: 21856643
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 29

Expert Comment

by:fosiul01
ID: 21856649
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
 
LVL 1

Expert Comment

by:Neoific
ID: 21856701
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
 

Author Comment

by:St_Aug_Beach_Bum
ID: 21856761
Wow, thank you all, I am testing.....
0
 
LVL 1

Expert Comment

by:morristhebear
ID: 21856771
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
 

Author Comment

by:St_Aug_Beach_Bum
ID: 21856821
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question