Solved

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

Posted on 2008-06-24
8
191 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

786 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