Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-24
8
Medium Priority
?
195 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 2000 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
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.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

704 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