View Single Post

  #1 (permalink)  
Old 01-19-2006
rlee0001
 
Posts: n/a
Default Looping algorithm to generate a string...

This is totally stupid but...I want to generate a string like:

"Showing all assets with a status of Ordered, Received or Placed."

The statuses are stored in an array and any number of them can be
expected (there are like 15 possible values and they are specifically
ordered). So the problem is that I want commas between the items except
the last two. I want an 'or' there.

Now I know I can code this up by looking ahead and it'll look very
messy but is there a known-optimal way to do this? Maybe counting the
records first and using a FOR loop for the first n-2 then outputing the
last two with the OR between them?

Anyone got the "perfect" answer?

What if you don't have an array but rather a bunch of seperate
variables like $name, $address, $age and so on? Whats the best way to
do this:

"Robert, Austin and 18 years old."

....when any of those variables are NULL or '' they are skipped so this
might also be output:

"Robert and Austin."

Like I said, its a stupid question.

-Robert

Reply With Quote