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. 7 13160
On Fri, 13 Jan 2006 16:24:03 -0800, "Bosconian" <bo*******@planetx.com> 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?
$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.
I tend to use something like:
print $count . ' noun' . ($count == 1 ? '' : 's')
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:4k********************************@4ax.com... On Fri, 13 Jan 2006 16:24:03 -0800, "Bosconian" <bo*******@planetx.com>
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?
$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.
I tend to use something like:
print $count . ' noun' . ($count == 1 ? '' : 's')
-- Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
That works nicely--thanks!
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);
....
<jo*********@sbcglobal.net> wrote in message
news:f9*****************@newssvr21.news.prodigy.co m... 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.
"Bosconian" <bo*******@planetx.com> wrote in message
news:_Z******************************@comcast.com. .. <jo*********@sbcglobal.net> wrote in message news:f9*****************@newssvr21.news.prodigy.co m... 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));
Bosconian said the following on 16/01/2006 21:03: <jo*********@sbcglobal.net> wrote in message news:f9*****************@newssvr21.news.prodigy.co m... 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.
--
Oli
Oli Filth (ca***@olifilth.co.uk) wrote:
: Bosconian said the following on 16/01/2006 21:03:
: >> <jo*********@sbcglobal.net> wrote in message
: >> news:f9*****************@newssvr21.news.prodigy.co m...
: >>> 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; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Trevor Best |
last post by:
I posted this from work but I don't think it got through so apologies if
this is duplicated.
I'm trying to avoid the old chestnut where you go something like:
Span = lngDays & " day(s)"
so...
|
by: Steve - DND |
last post by:
Just wondering if anyone out there has any code to convert a plural word to
it's singular form and vice versa. Most of our database tables are named in
a plural fashion. When we go to create...
|
by: Robert W. |
last post by:
In my current work I noticed that I have several circumstances where I need
to create little if/else constructs to handle the phrasing of a message.
This typically involves a ternary situation like...
|
by: Slypig |
last post by:
Can anyone enlighten me on how to include plural searches with a
fulltext mysql database search?
For example, if someone searches 'restaurant' I want it to search the
database for 'restaurant'...
|
by: Bill Norton |
last post by:
Over at PCMag.com there is a debate going on over the usability of CSS. The
discussion was initiated by an article by John Dvorak called "Why CSS Bugs
Me"...
|
by: eyh5 |
last post by:
Hi,
I'm wondering where I may get the C source code for a
linear-algebraic technique known as the singular value decomposition
(SVD).
I found the code (a function called svdcmp) from...
|
by: guate911 |
last post by:
Does anyone know of an open source applictation or API I can connect
to, that will allow me to find the plural of a word? For example, if I
type in "mouse", I need it to find the plural.. "mice".
...
|
by: tryce430 |
last post by:
Hi,
I've actually been working on a homework assignment which asks me to turn words into their plural forms. The first task is to check if the words are more than 3 characters and end in "us". If...
|
by: vinay3744 |
last post by:
Hi
I am currently working on figuring out a code for performing a singular value decomposition for String arrays.
Can some one help me get started on this. I have already done a declaration...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |