delphi123 Posted November 25, 2007 Share Posted November 25, 2007 Hi there, I'm creating a 'top ten' list that can be sorted by different values. Can anyone tell me if there is a way with php/mySQL to automatically change numbers to 'th' values. eg so 1,2,3,4 are converted to 1st, 2nd,3rd,4th? Quote Link to comment https://forums.phpfreaks.com/topic/78805-convert-1234-to-1st-2nd-3rd-4th-etc/ Share on other sites More sharing options...
revraz Posted November 25, 2007 Share Posted November 25, 2007 You probably have to write a function to read the last number and append the suffix. The Date function has it built in, but I don't know if there is anything built into PHP for just plain numbers. Quote Link to comment https://forums.phpfreaks.com/topic/78805-convert-1234-to-1st-2nd-3rd-4th-etc/#findComment-398812 Share on other sites More sharing options...
phpSensei Posted November 25, 2007 Share Posted November 25, 2007 Sorry i just read the last part of what you said, so i wrote this. Going to read the rest after i come back home... so here... <?php for($x = 1; $x <= 10; $x++){ if($x == 1){ echo $x . "st<br>"; } elseif($x == 2){ echo $x . "nd<br>"; } elseif($x == 3){ echo $x . "rd<br>"; } else { echo $x . "th<br>"; } } ?> With Mysql <?php $query = mysql_query("SELECT * FROM race ORDER BY place LIMIT 10"); $x = 1; while($row = mysql_fetch_array($query)){ if($x == 1){ echo $x . "st" .$row['racer']. "<br>"; } elseif($x == 2){ echo $x . "nd" .$row['racer']. "<br>"; } elseif($x == 3){ echo $x . "rd" .$row['racer']. "<br>"; } else { echo $x . "th " .$row['racer']. "<br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78805-convert-1234-to-1st-2nd-3rd-4th-etc/#findComment-398823 Share on other sites More sharing options...
phpSensei Posted November 25, 2007 Share Posted November 25, 2007 i am sorry, please delete this post for I did not know I was replying to myself. Quote Link to comment https://forums.phpfreaks.com/topic/78805-convert-1234-to-1st-2nd-3rd-4th-etc/#findComment-398837 Share on other sites More sharing options...
kenrbnsn Posted November 25, 2007 Share Posted November 25, 2007 If the number is between 1 and 31 (inclusive) you can use the date() function in conjunction with the strtotime() function: <?php $num = rand(1,31); // get a number echo date('jS',strtotime('2007-01-' . $num); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/78805-convert-1234-to-1st-2nd-3rd-4th-etc/#findComment-398838 Share on other sites More sharing options...
Barand Posted November 25, 2007 Share Posted November 25, 2007 try <?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>'; Quote Link to comment https://forums.phpfreaks.com/topic/78805-convert-1234-to-1st-2nd-3rd-4th-etc/#findComment-398848 Share on other sites More sharing options...
delphi123 Posted November 25, 2007 Author Share Posted November 25, 2007 WOW! Thanks for the tremendous replies folks! Wasn't expecting that! I think this'll work a treat! Quote Link to comment https://forums.phpfreaks.com/topic/78805-convert-1234-to-1st-2nd-3rd-4th-etc/#findComment-398856 Share on other sites More sharing options...
katie77 Posted November 25, 2007 Share Posted November 25, 2007 Hi there, I've been trying to use the following code, posted by Barand: function ordSuffix($i) { $str = "$i"; $t = $i > 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 orderSuffix(1).'<br>'; But I get the following error: Call to undefined function orderSuffix() Does anyone know how I can fix this? Many thanks, Katie Quote Link to comment https://forums.phpfreaks.com/topic/78805-convert-1234-to-1st-2nd-3rd-4th-etc/#findComment-398950 Share on other sites More sharing options...
Barand Posted November 25, 2007 Share Posted November 25, 2007 echo orderSuffix(1).'<br>'; The function is "ordSuffix()" Quote Link to comment https://forums.phpfreaks.com/topic/78805-convert-1234-to-1st-2nd-3rd-4th-etc/#findComment-398954 Share on other sites More sharing options...
katie77 Posted November 25, 2007 Share Posted November 25, 2007 Oh, silly me! Thank you It works perfectly now Quote Link to comment https://forums.phpfreaks.com/topic/78805-convert-1234-to-1st-2nd-3rd-4th-etc/#findComment-398958 Share on other sites More sharing options...
phpSensei Posted November 25, 2007 Share Posted November 25, 2007 If the number is between 1 and 31 (inclusive) you can use the date() function in conjunction with the strtotime() function: <?php $num = rand(1,31); // get a number echo date('jS',strtotime('2007-01-' . $num); ?> Ken What does this actually do? Quote Link to comment https://forums.phpfreaks.com/topic/78805-convert-1234-to-1st-2nd-3rd-4th-etc/#findComment-398978 Share on other sites More sharing options...
GingerRobot Posted November 25, 2007 Share Posted November 25, 2007 If the number is between 1 and 31 (inclusive) you can use the date() function in conjunction with the strtotime() function: <?php $num = rand(1,31); // get a number echo date('jS',strtotime('2007-01-' . $num); ?> Ken What does this actually do? It makes use of the date function's format to add the suffix onto the end of the date. The format jS is the day of the month, followed by the relevant suffix. The second parameter is to set a particular date for this. It uses strtotime to create the unix timestamp based on a date, which is an arbitary year and month (though, of course, you need to use a month with 31 days!) followed by the day of the month, which is the number we want. Quite a clever approach if you only need to go up to 31. Quote Link to comment https://forums.phpfreaks.com/topic/78805-convert-1234-to-1st-2nd-3rd-4th-etc/#findComment-398986 Share on other sites More sharing options...
phpSensei Posted November 25, 2007 Share Posted November 25, 2007 If the number is between 1 and 31 (inclusive) you can use the date() function in conjunction with the strtotime() function: <?php $num = rand(1,31); // get a number echo date('jS',strtotime('2007-01-' . $num); ?> Ken What does this actually do? It makes use of the date function's format to add the suffix onto the end of the date. The format jS is the day of the month, followed by the relevant suffix. The second parameter is to set a particular date for this. It uses strtotime to create the unix timestamp based on a date, which is an arbitary year and month (though, of course, you need to use a month with 31 days!) followed by the day of the month, which is the number we want. Quite a clever approach if you only need to go up to 31. Thats really neat. *Saved Script* Quote Link to comment https://forums.phpfreaks.com/topic/78805-convert-1234-to-1st-2nd-3rd-4th-etc/#findComment-398988 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.