PHP form results

Hello to all, first day on here and certainly will not be the last.

I ask that someone help me not become bald… errr… rather help me so I stop pulling my hair out.

Take this site for example.

http://www.tizag.com/phpT/examples/formexample.php

I understand the entire concept of this form in its primitive state. However, the part that is stumping me is how on earth can I make it seem grammitcally correct. ie. When the results echoed say you like Steak Pizza Chicken, how can I make it put comma’s and the word and in the sentence. So if someone only says they like steak and pizza it will echo “you like steak and pizza.” Or if they select all three for example, echo back saying “you like steak, pizza, and chicken.”

Any help would be much appreciated.

Hi there,

I’ve quickly knocked up this for you:

[code]<?php
function wordstolist($words,$wordsep = ", ")
{
switch(count($words))
{
case 0:
return ‘nothing’;
break;
case 1:
return $words[0];
break;
case 2:
return $words[0].’ and ‘.$words[1];
break;
default:
$comsep = array();
for($i=0;$i<count($words)-1;$i++)
{
$comsep[] = $words[$i];
}
return implode($wordsep,$comsep).’ and '.$words[$i++];
break;
}
}

echo ‘You like ‘.wordstolist($_POST[‘like’]).’.’;

?>


Chicken
Pizza
Steak
Pepperoni

[/code]

Using like[] in the name of an input means that each input with that name will just append its value to an array (with a key of “like”) into the $_POST array.

Sponsor our Newsletter | Privacy Policy | Terms of Service