Jump to content

[SOLVED] Numbering


Kingy

Recommended Posts

With my rank i am outputting something like

 

You are ranked XXst/nd/rd/th

 

eg:

1st, 2nd, 3rd, 4th

           if(($rank == 1) || ($rank == 21) || ($rank == 31)) {
             $suffix = "st";
           } elseif (($rank == 2) || ($rank == 22) || ($rank == 32)) {
             $suffix = "nd";
           } elseif (($rank == 3) || ($rank == 23) || ($rank == 33)) {
             $suffix = "rd";
           } else {
             $suffix = "th";
           }

i have that and it works, but is there simpler way of doing it so as my numbers grow i don't have to keep adding if $rank == 41,51,61 etc etc

 

Link to comment
Share on other sites

try

<?php
function my_suffix($b){
if ($a<0) $a = -$b; else $a =$b;
$s = array('th', 'st', 'nd');
$su = $a % 10;
if (((int)($a / 10)) % 10 == 1) $su = 0;
if ($su > 2) $su = 0;
return $s[$su];
}

echo my_suffix(191);
?>

Link to comment
Share on other sites

I did a search in Google and came up with this Ordinal number printer function

 

<?php
function ordinal($number) {

    // when fed a number, adds the English ordinal suffix. Works for any
    // number, even negatives

    if ($number % 100 > 10 && $number %100 < 14)
        $suffix = "th";
    else {
        switch($number % 10) {

            case 0:
                $suffix = "th";
                break;

            case 1:
                $suffix = "st";
                break;

            case 2:
                $suffix = "nd";
                break;

            case 3:
                $suffix = "rd";
                break;

            default:
                $suffix = "th";
                break;
        }

    }

    return "${number}<SUP>$suffix</SUP>";

}
?>

 

Ken

Link to comment
Share on other sites

ups

try

<?php
function my_suffix($b){
if ($a<0) $a = -$b; else $a =$b;
$s = array('th', 'st', 'nd', 'rd');
$su = $a % 10;
if (((int)($a / 10)) % 10 == 1) $su = 0;
if ($su > 3) $su = 0;
return $s[$su];
}

echo my_suffix(191);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.