|
Posted by Malcolm Dew-Jones on 09/26/05 11:37
Oli Filth (catch@olifilth.co.uk) wrote:
: Bosconian said the following on 16/01/2006 21:03:
: >> <john.d.mann@sbcglobal.net> wrote in message
: >> news:f9syf.9425$dW3.1196@newssvr21.news.prodigy.com...
: >>> Bosconian wrote:
: >>>> This might seem like a trivial thing, but has anyone has come up with
: >>>> a better way of outputting a singular vs. plural string?
: >>>>
: >>>>
: >>> Well, you can always put that code into a function in its own php file
: >>> and then just include it in your php file which will use it... Then you
: >>> could call it on one line each time you needed it. For example:
: >>>
: >>> include('printplural.php');
: >>> ...
: >>> print_plural($mystring, $db_result);
: >>> ...
: >
: > On second thought, breaking this out as a function proves useful:
: >
: > function print_plural($temp, $count) {
: > return $count . ' ' . $temp . ($count == 1 ? '' : 's');
: > }
: >
: > print print_plural('appointment', mysql_num_rows($result));
: >
: Of course, beware of non-standard pluralisation, e.g. "children",
: "geese", "bacteria", "fish", "mice", "oxen", etc.
don't get too tricky (untested)
function plural( $count, $word, $words )
{
if ($count > 1)) return $words;
else return $word;
}
e.g.
print "the ".plural($count,"customer","customers")
print "the ".plural($cooked,"goose" ,"geese")
Or use a lookup table to convert singles to plurals
# define array $plurals ahead of time with all the
# words that pluralizing
function plural( $count, $word )
{
if ($count >1) return $plurals[$word];
else return $word;
Navigation:
[Reply to this message]
|