Jump to content


Photo

adding the suffix to a rank


  • Please log in to reply
12 replies to this topic

#1 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 25 September 2006 - 10:05 AM

hi i have a ranking system,

1
2
3
4

etc, etc

but i would like

1st
2nd
3rd
4th...

can anyone help please?

#2 firelior

firelior
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 25 September 2006 - 10:30 AM

while $row=mysql_fetch_object($result){
$rank=$row->rank;
if $rank==1{
$rank=$rank."st";
}
if $rank==2{
$rank=$rank."nd";
}
if $rank==3{
$rank=$rank."rd";
}
else{
$rank=$rank."th";
}
}

#3 Thierry

Thierry
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationNetherlands

Posted 25 September 2006 - 10:37 AM

This wont completely cover it, say that a 21 shows up...
Since you need to get the last number of the rank...

$rank_number = substr($rank,-1,1);

//now use this to check all the ranks...
if $rank_number==1{
$rank=$rank."st";
}
if $rank_number==2{
$rank=$rank."nd";
}
if $rank_number==3{
$rank=$rank."rd";
}
else{
$rank=$rank."th";
}


Have you tried....a reset?

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 25 September 2006 - 11:00 AM

notwithstanding 11, 12, 13......

if ( $rank > 3 && $rank < 21)
{
$rank .= 'th';
}
else
{
$rank_number = 1substr($rank,-1,1);

switch ($rank_numer)
{
case 1:
  $rank .= 'st';
  break;
case 2:
  $rank .= 'nd';
  break;
case 3:
  $rank .= 'rd';
  break;
default:
  $rank .= 'th';
}
follow me on twitter @PHPsycho

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 25 September 2006 - 11:01 AM

The only reference I could find in the manual is when used in a date format string (eg "jS"), so

<?php
function ordSuffix($n) {
    $str = "$n";
    $t = $n > 9 ? substr($str,-2,1) : 0;
    $u = substr($str,-1);
    if ($t==1) return $str . 'th';
    else switch ($u) {
        case 1: return $str . 'st';
        case 2: return $str . 'nd';
        case 3: return $str . 'rd';
        default: return $str . 'th';
    }
}

echo ordSuffix(1).'<br>';
echo ordSuffix(11).'<br>';
echo ordSuffix(101).'<br>';
echo ordSuffix(22).'<br>';
echo ordSuffix(33).'<br>';
echo ordSuffix(44).'<br>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 25 September 2006 - 12:17 PM

thank you very much

#7 Thierry

Thierry
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationNetherlands

Posted 25 September 2006 - 01:50 PM

notwithstanding 11, 12, 13......

if ( $rank > 3 && $rank < 21)
{
$rank .= 'th';
}
else
{
$rank_number = 1substr($rank,-1,1);

switch ($rank_numer)
{
case 1:
  $rank .= 'st';
  break;
case 2:
  $rank .= 'nd';
  break;
case 3:
  $rank .= 'rd';
  break;
default:
  $rank .= 'th';
}


At that rate we could also be going on about 111, 112 and 113 :P
Have you tried....a reset?

#8 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 26 September 2006 - 11:03 AM

$rank = 1;

$adverts_query = "SELECT * FROM adverts ORDER BY adverts_link_amount DESC, adverts_link_date ASC";
$adverts_result = mysql_query($adverts_query) or die (mysql_error());    

    while ($adverts_array = mysql_fetch_array($adverts_result, MYSQL_ASSOC))
    {
   
$adverts_id = $adverts_array['adverts_id']; 
    $adverts_link = $adverts_array['adverts_link'];
$adverts_link_url = $adverts_array['adverts_link_url'];
$adverts_link_amount = number_format($adverts_array['adverts_link_amount']);

$rank = ordSuffix($rank); 
     
echo"    <tr>
          <td align='center' class='text_bold' height='25'>$rank</td>
          <td align='center' class='text_bold'><a href='$config_website_url/$config_website_url_topic/files/url.php?id=$adverts_id' class='blue_bold_none' target='_blank'>$adverts_link</a></td>
          <td align='center' class='text_bold'>£ $adverts_link_amount</td>          
        </tr>";

$rank ++;
       
        }

i have used that and wat barrand showed me, yesterday i thought it worked but today i have noticed it doesnt.....

check http://www.links-bid...x.php?page=home

to see what I get....

#9 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 26 September 2006 - 11:09 AM

I have never seen 111st or 112nd...

But hey maybe someone will invent that.

But on reflection....

101th is not very good! so I think.....

$trank = $trank > 9 ? substr($rank,-2,1) : $rank;

if ( $trank > 3 && $trank < 21)
{
$rank .= 'th';
}
else
{
$rank_number = substr($trank,-1,1);

switch ($rank_numer)
{
case 1:
  $rank .= 'st';
  break;
case 2:
  $rank .= 'nd';
  break;
case 3:
  $rank .= 'rd';
  break;
default:
  $rank .= 'th';
}

OOOOOOOOOOPS my bad. barand has done it.


NOTE TO SELF : Read entire thread....
follow me on twitter @PHPsycho

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 26 September 2006 - 11:14 AM

That's because you are incrementing the "1st" and not the "1"

Wrong way
<?php
$rank = 1;
for ($i=0; $i<10; $i++) {
    $rank = ordSuffix($rank);
    echo $rank, '<br>';
    $rank++;
}
?>

Now try this (right) way,
<?php
$rank = 1;
for ($i=0; $i<10; $i++) {
    $rankStr = ordSuffix($rank);
    echo $rankStr, '<br>';
    $rank++;
}

?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 26 September 2006 - 11:20 AM

    for ($i=0; $i<10; $i++)
{

while ($adverts_array = mysql_fetch_array($adverts_result, MYSQL_ASSOC))
    {
   
$adverts_id = $adverts_array['adverts_id']; 
    $adverts_link = $adverts_array['adverts_link'];
$adverts_link_url = $adverts_array['adverts_link_url'];
$adverts_link_amount = number_format($adverts_array['adverts_link_amount']);
   
$rankStr = ordSuffix($rank);
     
echo"    <tr>
          <td align='center' class='text_bold' height='25'>$rankStr</td>
          <td align='center' class='text_bold'><a href='$config_website_url/$config_website_url_topic/files/url.php?id=$adverts_id' class='blue_bold_none' target='_blank'>$adverts_link</a></td>
          <td align='center' class='text_bold'>£ $adverts_link_amount</td>          
        </tr>";

$rank ++;
       
        }

}

is that the right way to use it barand?

#12 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 26 September 2006 - 11:26 AM

You don't need the for() loop, that was just for my sample to generate a few values. You already have the loop.

<?php
$rank = 1;
while ($adverts_array = mysql_fetch_array($adverts_result, MYSQL_ASSOC))
        {
        
       $adverts_id = $adverts_array['adverts_id']; 
        $adverts_link = $adverts_array['adverts_link'];
       $adverts_link_url = $adverts_array['adverts_link_url'];
       $adverts_link_amount = number_format($adverts_array['adverts_link_amount']);
              
       $rankStr = ordSuffix($rank);
          
echo"    <tr>
          <td align='center' class='text_bold' height='25'>$rankStr</td>
          <td align='center' class='text_bold'><a href='$config_website_url/$config_website_url_topic/files/url.php?id=$adverts_id' class='blue_bold_none' target='_blank'>$adverts_link[/url]</td>
          <td align='center' class='text_bold'>£ $adverts_link_amount</td>            
         </tr>";
      
       $rank ++;
                   
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#13 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 26 September 2006 - 11:42 AM

thanks very much




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users