This is a discussion on Looping algorithm to generate a string... within the alt.comp.lang.php forums, part of the PHP Programming Forums category; This is totally stupid but...I want to generate a string like: "Showing all assets with a status of ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
rlee0001 wrote: > 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? I don't know if it's perfect, but it'll work. And it isn't /that/ stupid :) $a = array('Ordered', 'Received', 'Placed'); $str = implode(', ', $a); $pos = strrpos($str, ','); $str = substr($str, 0, $pos) . ' or' . substr($str, ($pos+1)); $str = "Showing all assets with a status of $str"; Of course, that doesn't account for the corner cases of 0 or 1 items in the array. |
|
|||
|
Thats pretty cool. I didn't even think of implode and would have used a
loop. Maybe I'll write a function that uses implode along with your code to insert 'or' and a check for the possibility of 0 or 1 array element. Something like: explode_x (seperator, array) ....where seperator is something like 'and' or 'or'. I ultimately want to generate a human readable paragraph which describes an advanced search which contains multiple fields, some of which are arrays. Showing all assets with a status of Ordered, Received or Placed, assigned to a user whose last name begins with either 'Smith' or 'D' and first name begins with 'J' on purchase order 374238956. Notice that although the purchase order was the last field in the list that 'and' was not used but rather 'on'. And that since last name was an array an 'either' was inserted while an either was not inserted in first name since it contained only one value. But status doesn't get an 'either' either way. Also last name and first name were seperated by an 'and' even though first name isn't the last field in the list. And althought last name and first name can be specified independantly of one-another there is only one prefix 'where the user whose' included before the both of them. And equally valid output for a different set of specified fields could have been: Showing all assets of type Desktop or Laptop, assigned to a user whose first name begins with 'J', with a MAC address which starts with 'de:ad:be' and which counts as a seat. Any field can be omitted and other fields may have been specified. Something this complex might be impossible. But I'm sure that where there is a will there is a way. Has anyone done anything like this before. <-- Another stupid question. -Robert |
|
|||
|
"rlee0001" <robeddielee@hotmail.com> wrote in message news:1137633575.690364.86380@g47g2000cwa.googlegro ups.com... > 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. How about... $array = array('Ordered', 'Received', 'Placed'); $list = join($array, ', '); $lastComma = strrpos($list, ','); print substr_replace($list, ' or', $lastComma, 1); Balazs |
|
|||
|
"rlee0001" <robeddielee@hotmail.com> wrote in message news:1137633575.690364.86380@g47g2000cwa.googlegro ups.com... > This is totally stupid but...I want to generate a string like: > > "Showing all assets with a status of Ordered, Received or Placed." current grammar rules when I was in college 10 years ago was that it should be "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 > |
|
|||
|
"Jim Michaels" <jmichae3@nospam.yahoo.com> wrote in message news:E5mdneObcL9xHXjenZ2dnUVZ_tKdnZ2d@comcast.com. .. > > "rlee0001" <robeddielee@hotmail.com> wrote in message > news:1137633575.690364.86380@g47g2000cwa.googlegro ups.com... >> This is totally stupid but...I want to generate a string like: >> >> "Showing all assets with a status of Ordered, Received or Placed." > > current grammar rules when I was in college 10 years ago was that it > should be > "Showing all assets with a status of Ordered, Received, or Placed." given the earlier code shown, it's an easy mod (needs a space, too). $array = array('Ordered', 'Received', 'Placed'); $list = join($array, ', '); $lastComma = strrpos($list, ','); print substr_replace($list, ', or ', $lastComma, 1); > >> >> 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 >> > > |