I would like create a conditional to turn '1 Search Results' (plural) into '1 Search Result' (singular) using PHP. Obviously if more than one result is created, I'd like to keep the plural version.
My site is built using Expression Engine, but I'm sure this is a common PHP conditional.
My code looks like this:
<h2><em>{exp:s开发者_如何学编程earch:total_results}{total}{/exp:search:total_results} search results for</em> “{exp:search:keywords}”</h2>
Any help is appreciated!
This will do the trick:
<h2><em>{exp:search:total_results}{total_results}{/exp:search:total_results} result{if "{exp:search:total_results}" != 1}s{/if} for</em> “{exp:search:keywords}”</h2>
Well in normal php coding I use this:
$display_results = ($results == 1) ? '1 Search Result' : $results .' Search Results';
$results being the count return from the db
Ternary logic is probably the way to go on this one.
$text = "$total_results Search Result" . ( $total_results != 1 ? 's' : '' );
If you're not familiar with ternary logic, the above statement does the same thing as:
$text = "$total_results Search Result";
if ($total_results != 1) { $text = $text . 's'; }
精彩评论