views:

198

answers:

3

If I have an array that looks like this:

$str = '';
if( $_POST['first'] )
    $str = $_POST['first'];
if( $_POST['second'] )
    $str .= ($str != '' ? ',' : '') . $_POST['second'];
if( $_POST['third'] )
    $str .= ($str != '' ? ',' : '') . $_POST['third'];
if( $_POST['fourth'] )
    $str .= ($str != '' ? ',' : '') . $_POST['second'];
$str .= ($str != '' ? '.' : '');

Which gives me something like this:

Joe, Adam, Mike.

However, I would like to add an "and" before the last item.

So it would then read:

Joe, Adam, and Mike.

How can I modify my code to do this?

+9  A: 

Arrays are awesome for this:

$str = array();
foreach (array('first','second','third','fourth') as $k) {
    if ($_POST[$k]) {
        $str[] = $k;
    }
}
$last = array_pop($str);
echo implode(", ", $str) . " and " . $last;

You should probably special case the above for when there's one item. I, in fact, wrote a function called "conjunction" which does the above, and includes the special case:

function conjunction($x, $c="or")
{
    if (count($x) <= 1) {
        return implode("", $x);
    }
    $ll = array_pop($x);
    return implode(", ", $x) . " $c $ll";
}

Nice question!

Updated: General purpose way to do this:

function and_form_fields($fields)
{
     $str = array();
     foreach ($fields as $k) {
         if (array_key_exists($k, $_POST) && trim($_POST[$k])) {
              $str[] = $k;
         }
     }
     return conjunction($str, "and");
}

...

and_form_fields(array("Name_1","Name_2",...));

I added correct $_POST checking to avoid notices, and blank values.

razzed
Nice answer! +1
alex
Plus your function definition rhymes :P
alex
The above only returns:first, second, third and fourthI would like to add the last comma, often called the serial comma.How can I modify the above array to accomplish this?Thanks guys!
I think the serial comma is technically not correct English, but, just change the last line to: ", and " . $last--OR--", $c $ll";B
razzed
Thanks yet again! I'm sorry for all my questions.. So I have a form with textfields (5) and depending on if the user enters something in, those are carried to the second page (just 1 or any combination) where I want to write out using the above function. If my textfield forms are called "Name_1" "Name_2" etc.. how would I use the function to request those form items? I thank you again!! I've never played with functions before and I understand now what you mean by "special case."
See updated answer: and_form_fields ... on your second page.
razzed
A: 

The first ideea that comes to my mind is to always keep a last one name in an auxiliar variabile. When you have another one to put in, you take it put the 'comma' and get the next name in the aux.

At the end, when you finished added the names, you add 'and' and the last name from aux.

Timotei Dolean
A: 

Instead of posting each as a separate variable, why not post them as an array:

#pull the array from the POST:
$postedarray = $_POST['names'];

#count the number of elements in the posted array:
$numinarray = count($postedarray);

#subtract 1 from the number of elements, because array elements start at 0
$numinarray = $numinarray -1;

#set a prepend string
$prependstr = "and ";

#Pull the last element of the array
$lastname = $postedarray[$numinarray];

#re-define the last element to contan the prepended string
$postedarray[$numinarray] = "$prependstr$lastname";

#format the array for your requested output
$comma_separated = implode(", ", $postedarray);

print "$comma_separated";
Russell242