Jump to content

Archived

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

brown2005

adding the suffix to a rank

Recommended Posts

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";
}
}

Share this post


Link to post
Share on other sites
This wont completely cover it, say that a 21 shows up...
Since you need to get the last number of the rank...

[code]
$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";
}

[/code]

Share this post


Link to post
Share on other sites
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';
}

Share this post


Link to post
Share on other sites
The only reference I could find in the manual is when used in a date format string (eg "jS"), so

[code]<?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>';
?>
[/code]

Share this post


Link to post
Share on other sites
[quote author=ToonMariner link=topic=109379.msg440792#msg440792 date=1159182052]
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';
}
[/quote]

At that rate we could also be going on about 111, 112 and 113 :P

Share this post


Link to post
Share on other sites
$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.com/index/index.php?page=home

to see what I get....

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
That's because you are incrementing the "1st" and not the "1"

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

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

?>[/code]

Share this post


Link to post
Share on other sites
    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?

Share this post


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

[code]<?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 ++;
                 
}
?>[/code]

Share this post


Link to post
Share on other sites

×

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.