Solved

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

Posted on 2008-06-24
8
193 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

739 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