473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

singular or plural output string

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.
Jan 14 '06 #1
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
Jan 14 '06 #2
"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!
Jan 14 '06 #3
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);
....
Jan 15 '06 #4
<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.
Jan 16 '06 #5
"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));
Jan 16 '06 #6
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
Jan 17 '06 #7
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;

Jan 17 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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...
2
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...
2
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...
0
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'...
31
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"...
2
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...
4
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". ...
1
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...
1
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...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.