PHP: Dynamic Term

I’m guessing there’s a generic PHP technique for doing this.

The below code returns a listing of articles with a brief message. If there are multiple results (2+) the plural articles is grammatically correct, but if there’s only one result it isn’t.

So I would like to dynamically modify the term (singular or plural) as appropriate depending on the total.

[[!+results:notempty=`<h1>Nice, [[+contributor]]. You’ve written [[+total]] articles.</h1>`]]

Todd
https://xiiro.com


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Sure, this is pretty simple as long as the word differs in singular and plural form by the letter “s” tacked onto the end. If you need more than that, there are a number of great clones of Ruby’s pluralize() method in PHP, I often use the one from CakePHP when I work in PHP and need the ActiveSupport spice.

<?php
	function pluralize($count, $singular){
		return $count . ' ' . (($count == 1) ? $singular : "{$singular}s");
	} 
?>

Walter

On Jan 2, 2015, at 3:44 PM, Todd email@hidden wrote:

I’m guessing there’s a generic PHP technique for doing this.

The below code returns a listing of articles with a brief message. If there are multiple results (2+) the plural articles is grammatically correct, but if there’s only one result it isn’t.

So I would like to dynamically modify the term (singular or plural) as appropriate depending on the total.

[[!+results:notempty=`<h1>Nice, [[+contributor]]. You’ve written [[+total]] articles.</h1>`]]

Todd
https://xiiro.com


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thank you.

I put the code into a snippet and named it, [[Pluralize]]. I then placed it inline right after the term I want to modify, article but nothing. Clearly I’m not using it correctly.

[[!+results:notempty=`<h2>Woo-hoo! I found <span class="sisea-highlight">[[+total]]</span> article[[!Pluralize]].</h2>`]]

Todd

<?php function pluralize($count, $singular){ return $count . ' ' . (($count == 1) ? $singular : "{$singular}s"); } ?>

Walter

[[!+results:notempty=`<h1>Nice, [[+contributor]]. You’ve written [[+total]] articles.</h1>`]]

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

In case you’re curious, I went with a different approach,

Snippet

return  $count > 1?  's' : “";

Message

article[[!pluralFilter? &count=`[[+total]]`]]

Thanks, T.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I don’t know how you pass an argument to a function in a snippet, but you have to pass the count and the singular to the function:

<?php print pluralize(12, 'Banana'); ?>

That prints out 12 Bananas when you call it like that.

Walter

On Jan 3, 2015, at 11:19 AM, Todd email@hidden wrote:

Thank you.

I put the code into a snippet and named it, [[Pluralize]]. I then placed it inline right after the term I want to modify, article but nothing. Clearly I’m not using it correctly.

[[!+results:notempty=`<h2>Woo-hoo! I found <span class="sisea-highlight">[[+total]]</span> article[[!Pluralize]].</h2>`]]

Todd

<?php function pluralize($count, $singular){ return $count . ' ' . (($count == 1) ? $singular : "{$singular}s"); } ?>

Walter

[[!+results:notempty=`<h1>Nice, [[+contributor]]. You’ve written [[+total]] articles.</h1>`]]

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options