brown2005 Posted September 25, 2006 Share Posted September 25, 2006 hi i have a ranking system,1234etc, etcbut i would like1st2nd3rd4th...can anyone help please? Quote Link to comment Share on other sites More sharing options...
firelior Posted September 25, 2006 Share Posted September 25, 2006 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";}} Quote Link to comment Share on other sites More sharing options...
Thierry Posted September 25, 2006 Share Posted September 25, 2006 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] Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 25, 2006 Share Posted September 25, 2006 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 Link to comment Share on other sites More sharing options...
Barand Posted September 25, 2006 Share Posted September 25, 2006 The only reference I could find in the manual is when used in a date format string (eg "jS"), so[code]<?phpfunction 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] Quote Link to comment Share on other sites More sharing options...
brown2005 Posted September 25, 2006 Author Share Posted September 25, 2006 thank you very much Quote Link to comment Share on other sites More sharing options...
Thierry Posted September 25, 2006 Share Posted September 25, 2006 [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 Quote Link to comment Share on other sites More sharing options...
brown2005 Posted September 26, 2006 Author Share Posted September 26, 2006 $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=hometo see what I get.... Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 26, 2006 Share Posted September 26, 2006 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.... Quote Link to comment Share on other sites More sharing options...
Barand Posted September 26, 2006 Share Posted September 26, 2006 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] Quote Link to comment Share on other sites More sharing options...
brown2005 Posted September 26, 2006 Author Share Posted September 26, 2006 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 26, 2006 Share Posted September 26, 2006 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] Quote Link to comment Share on other sites More sharing options...
brown2005 Posted September 26, 2006 Author Share Posted September 26, 2006 thanks very much Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.