singular or plural output string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bosconian

    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.


  • Andy Hassall

    #2
    Re: singular or plural output string

    On Fri, 13 Jan 2006 16:24:03 -0800, "Bosconian" <bosconian@plan etx.com> wrote:
    [color=blue]
    >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.[/color]

    I tend to use something like:

    print $count . ' noun' . ($count == 1 ? '' : 's')

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • Bosconian

      #3
      Re: singular or plural output string

      "Andy Hassall" <andy@andyh.co. uk> wrote in message
      news:4khgs1tf5q ao7mdc7r1t72753 n5ov22mrj@4ax.c om...[color=blue]
      > On Fri, 13 Jan 2006 16:24:03 -0800, "Bosconian" <bosconian@plan etx.com>[/color]
      wrote:[color=blue]
      >[color=green]
      > >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.[/color]
      >
      > I tend to use something like:
      >
      > print $count . ' noun' . ($count == 1 ? '' : 's')
      >
      > --
      > Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
      > http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool[/color]

      That works nicely--thanks!


      Comment

      • john.d.mann@sbcglobal.net

        #4
        Re: singular or plural output string

        Bosconian wrote:[color=blue]
        > 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.
        >
        >[/color]

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

        Comment

        • Bosconian

          #5
          Re: singular or plural output string

          <john.d.mann@sb cglobal.net> wrote in message
          news:f9syf.9425 $dW3.1196@newss vr21.news.prodi gy.com...[color=blue]
          > Bosconian wrote:[color=green]
          > > 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.
          > >
          > >[/color]
          >
          > 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);
          > ...[/color]

          I wrap reusable code in functions all the time, but in this case it's not
          needed.


          Comment

          • Bosconian

            #6
            Re: singular or plural output string

            "Bosconian" <bosconian@plan etx.com> wrote in message
            news:_ZudnWhCXc iPmVHenZ2dnUVZ_ tWdnZ2d@comcast .com...[color=blue]
            > <john.d.mann@sb cglobal.net> wrote in message
            > news:f9syf.9425 $dW3.1196@newss vr21.news.prodi gy.com...[color=green]
            > > Bosconian wrote:[color=darkred]
            > > > This might seem like a trivial thing, but has anyone has come up with[/color][/color][/color]
            a[color=blue][color=green][color=darkred]
            > > > 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[/color][/color][/color]
            expressions)[color=blue][color=green][color=darkred]
            > > > without resorting to something lame like "1 appointment(s)" ?
            > > >
            > > > Again, no biggie and the above syntax is tried and true, but it's[/color][/color][/color]
            always[color=blue][color=green][color=darkred]
            > > > nice to learn a new trick now and then.
            > > >
            > > > Thanks.
            > > >
            > > >[/color]
            > >
            > > 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);
            > > ...[/color]
            >
            > I wrap reusable code in functions all the time, but in this case it's not
            > needed.
            >
            >[/color]

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


            Comment

            • Oli Filth

              #7
              Re: singular or plural output string

              Bosconian said the following on 16/01/2006 21:03:[color=blue][color=green]
              >> <john.d.mann@sb cglobal.net> wrote in message
              >> news:f9syf.9425 $dW3.1196@newss vr21.news.prodi gy.com...[color=darkred]
              >>> 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);
              >>> ...[/color][/color]
              >
              > 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));
              >[/color]

              Of course, beware of non-standard pluralisation, e.g. "children",
              "geese", "bacteria", "fish", "mice", "oxen", etc.


              --
              Oli

              Comment

              • Malcolm Dew-Jones

                #8
                Re: singular or plural output string

                Oli Filth (catch@olifilth .co.uk) wrote:
                : Bosconian said the following on 16/01/2006 21:03:
                : >> <john.d.mann@sb cglobal.net> wrote in message
                : >> news:f9syf.9425 $dW3.1196@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;



                Comment

                Working...