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
https://forums.phpfreaks.com/topic/87325-solved-numbering/
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
https://forums.phpfreaks.com/topic/87325-solved-numbering/#findComment-446662
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.