You are here: Re: singular or plural output string « PHP Programming Language « IT news, forums, messages
Re: singular or plural output string

Posted by Bosconian on 09/28/90 11:37

"Bosconian" <bosconian@planetx.com> wrote in message
news:_ZudnWhCXciPmVHenZ2dnUVZ_tWdnZ2d@comcast.com...
> <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?
> > >
> > > For example:
> > >
> > > // default plural label
> > > $string = "appointments";
> > > // singular label
> > > if (mysql_num_rows($results) == 1) $string = "appointment";
> > > print mysql_num_rows($results) . $string;
> > >
> > > $results always contain 1 or more records so "0 appointments" is not
> > > applicable.
> > >
> > > Is there a way to do this in one line (perhaps with regular
expressions)
> > > without resorting to something lame like "1 appointment(s)"?
> > >
> > > Again, no biggie and the above syntax is tried and true, but it's
always
> > > nice to learn a new trick now and then.
> > >
> > > Thanks.
> > >
> > >
> >
> > 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);
> > ...
>
> I wrap reusable code in functions all the time, but in this case it's not
> needed.
>
>

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));

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация