473,794 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = "appointmen ts";
// singular label
if (mysql_num_rows ($results) == 1) $string = "appointmen t";
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 13183
On Fri, 13 Jan 2006 16:24:03 -0800, "Bosconian" <bo*******@plan etx.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.u k :: 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.c om...
On Fri, 13 Jan 2006 16:24:03 -0800, "Bosconian" <bo*******@plan etx.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.u k :: 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 = "appointmen ts";
// singular label
if (mysql_num_rows ($results) == 1) $string = "appointmen t";
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('printp lural.php');
....
print_plural($m ystring, $db_result);
....
Jan 15 '06 #4
<jo*********@sb cglobal.net> wrote in message
news:f9******** *********@newss vr21.news.prodi gy.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 = "appointmen ts";
// singular label
if (mysql_num_rows ($results) == 1) $string = "appointmen t";
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('printp lural.php');
...
print_plural($m ystring, $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*******@plan etx.com> wrote in message
news:_Z******** *************** *******@comcast .com...
<jo*********@sb cglobal.net> wrote in message
news:f9******** *********@newss vr21.news.prodi gy.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 = "appointmen ts";
// singular label
if (mysql_num_rows ($results) == 1) $string = "appointmen t";
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('printp lural.php');
...
print_plural($m ystring, $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($t emp, $count) {
return $count . ' ' . $temp . ($count == 1 ? '' : 's');
}

print print_plural('a ppointment', mysql_num_rows( $result));
Jan 16 '06 #6
Bosconian said the following on 16/01/2006 21:03:
<jo*********@sb cglobal.net> wrote in message
news:f9******** *********@newss vr21.news.prodi gy.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('printp lural.php');
...
print_plural($m ystring, $db_result);
...


On second thought, breaking this out as a function proves useful:

function print_plural($t emp, $count) {
return $count . ' ' . $temp . ($count == 1 ? '' : 's');
}

print print_plural('a ppointment', 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*********@sb cglobal.net> wrote in message
: >> news:f9******** *********@newss vr21.news.prodi gy.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('printp lural.php');
: >>> ...
: >>> print_plural($m ystring, $db_result);
: >>> ...
: >
: > On second thought, breaking this out as a function proves useful:
: >
: > function print_plural($t emp, $count) {
: > return $count . ' ' . $temp . ($count == 1 ? '' : 's');
: > }
: >
: > print print_plural('a ppointment', 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","cu stomers")
print "the ".plural($cooke d,"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
2204
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 that an s gets appended only if it's more than 1, I came up with this: Span = lngDays & " day" & String(Abs(Sgn(lngDays - 1)), "s")
2
6833
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 templates to select a single record, we end up with a class name that sounds as if it's a collection instead of just one data object. Just wondering if anyone has a nice workaround. I started working on something, but it quickly turned into a large...
2
2301
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 this example illustrates: 0 files were downloaded (or No files were downloaded) 1 file was downloaded 2 files were downloaded They're all very similar but each is subtlely different. I soon realized that I could write a few related methods...
0
2065
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' and 'restaurants' Regards,
31
2546
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" (http://www.pcmag.com/article2/0,1759,1987181,00.asp). The discussion starts here: http://discuss.pcmag.com/forums/1/1004331343/ShowPost.aspx#1004331343. Someone suggested that some of you folks might have a thing or two to say about the topic, so I'm inviting everyone to drop by and...
2
9301
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 Numerical Recipes in C (by Press, Teukolskyk, Vetterling, and Flannery). However, it always yields a wrong n-by-n V matrix after decomposing the original m-by-n matrix A. I am beginning to suspect that the svdcmp provided in
4
3031
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". Thanks
1
6268
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 it does I just take out the us and add an "i" at the end. Otherwise I just throw an "s" to the end of the word. I think I got most of the program down, but for some reason it gives me an error with &&. Could someone take a look at my code and tell...
1
2272
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 for the array of dimension 5000X3000. Now i need to perform the function on the array. Thanks a lot in advance. Vinay3744
0
10433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10161
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.