Posts Tagged ‘geeky’

Roman Numerals in ColdFusion

Saturday, April 11th, 2009

I think Roman Numerals are particuarly attractive when used well on the web.

This website uses Roman Numerals for the copyright date. Of course, this is easily implemented in PHP with the code below:

<?php
 
function numberToRoman($num) 
 {
     // Make sure that we only use the integer portion of the value
     $n = intval($num);
     $result = '';
 
     // Declare a lookup array that we will use to traverse the number:
     $lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
     'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
     'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
 
     foreach ($lookup as $roman => $value) 
     {
         // Determine the number of matches
         $matches = intval($n / $value);
 
         // Store that many characters
         $result .= str_repeat($roman, $matches);
 
         // Substract that from the number
         $n = $n % $value;
     }
 
     // The Roman numeral should be built, return it
     return $result;
 }
 
echo(numberToRoman(date('Y')))
 
?>

In ColdFusion, though, I have not been able to find a ‘numberToRoman’ function anywhere, so had to build my own.

Solution

The ‘RomanNumeralsFormat’ function utilises another function I created called ‘RoundDown’. First, we declare the latter:

<cfscript>
function RoundDown(numberIn,roundBy){
	var resultantNumber = 0;
 
	if( (numberIn MOD roundBy) NEQ 0 )
	{
		resultantNumber = numberIn - (numberIn MOD roundBy);
	}else{
		resultantNumber = numberIn;
	}
 
	return resultantNumber;
}
</cfscript>

Then RomanNumeralsFormat is ready to be declared:

<cffunction name="RomanNumeralsFormat" returntype="string" displayname="Convert to roman numerals">
    <cfargument name="number" type="numeric" required="yes" displayname="The required number to pass in">
 
    <cfset numberOrig = number>
    <cfset number = round(number)>
 
    <cfset onesChrs = "I,II,III,IV,V,VI,VII,VIII,IX">
    <cfset tensChrs = "X,XX,XXX,XL,L,LX,LXX,LXXX,XC">
    <cfset hundredsChrs = "C,CC,CCC,CD,D,DC,DCC,DCCC,CM">
    <cfset thousandsChrs = "M,MM,MMM,MMMM">
 
    <cfif number GT 4999>
        <cfreturn numberOrig>
		<cfthrow message="Cannot represent numbers larger than 4999 in plain ASCII.">
	<cfelseif number EQ 0>
		<!---
		(nullae)
		--->
		<cfreturn "N">    
    <cfelse>
		<cfset result = "">
 
    	<cfset thousandsCount = RoundDown(number,1000,'down') / 1000>
		<cfif thousandsCount gt 0>
             	<cfset result = listGetAt(thousandsChrs,thousandsCount)>
		</cfif>
 
    	<cfset hundredsCount = RoundDown(number-(1000*thousandsCount),100,'down') / 100>
		<cfif hundredsCount gt 0>
	    	<cfset result = result & listGetAt(hundredsChrs,hundredsCount)>
        </cfif>
 
    	<cfset tensCount = RoundDown(number-(1000*thousandsCount)-(100*hundredsCount),10,'down') / 10>
		<cfif tensCount gt 0>
	    	<cfset result = result & listGetAt(tensChrs,tensCount)>
		</cfif>
 
    	<cfset onesCount = RoundDown(number-(1000*thousandsCount)-(100*hundredsCount)-(10*tensCount),1,'down')>
		<cfif onesCount gt 0>
	    	<cfset result = result & listGetAt(onesChrs,onesCount)>
       	</cfif>
 
        <cfreturn result>
    </cfif>
</cffunction>

We call the function like this:

<cfdump var="#RomanNumeralsFormat('999')#"> <!--- Produces: CMXCIX --->
<cfdump var="#RomanNumeralsFormat('2009')#"> <!--- Produces: MMIX --->
<cfdump var="#RomanNumeralsFormat('2008.9')#"> <!--- Rounds to 2009 and Produces: MMIX --->
<cfdump var="#RomanNumeralsFormat('11.11')#"> <!--- Rounds to 11 and Produces: XI --->
<cfdump var="#RomanNumeralsFormat('11.99')#"> <!--- Rounds to 12 and Produces: XII --->

I hope you find this post interesting and the function can be of use to you. ;-)

ISAPI Rewrite and WordPress

Thursday, April 9th, 2009

This blog uses the WordPress blogging platform which I host myself on my Windows server in the US.

However, WordPress support for ‘pretty URLs’ (pretty Permalinks) does not seem to exist Windows servers that use ISAPI Rewrite for re-writing URLs.

An example of a pretty v. ugly URL is:

  • Pretty:
    http://www.example.com/general/isapi-rewrite-and-wordpress
  • Ugly:
    http://www.example.com/?categoryname=general&p=123

URL re-writing is the process whereby you websites’ users (i.e. you guys as readers of my blog) can type in a pretty URL, and because the URL is re-written, the web server finds the right page at the ugly URL and sends it to your screen.

For those that don’t know, this re-writing is considered good practice, because it means that URLs are more aesthetically pleasing to read, easier to remember,  and easier for search engines to find.

[Also, if you are reading this post out of interest rather than for practical advice, then you may be asking what on earth is ISAPI Rewrite? Well it is simply a piece of software which is installed on a webserver that runs a Windows operating system. It allows for URL re-writing and is easily setup with one small file called 'httpd.ini' which is a settings/configuration file that website designers upload to their website.]

Problem

WordPress does not allow for pretty Permalinks customizations to work with a Windows server and ISAPI Rewrite installed to re-writing URLs.

Solution

[Should work with all WP versions, ATW I use 2.71]

  1. Customise your permalinks as you want (at http://yourblog/wp-admin/options-permalink.php)
    e.g. I use custom structure “/%category%/%postname%” - this creates the Pretty Permalink as in the example above. [More information on choosing your Permalinks.]


    Step 1

  2. Install the plugin (how to?) called ‘AskApache RewriteRules Viewer‘. Next, activate the plugin.
  3. Go to your WP Admin Settings page (at http://yourblog//wp-admin/options-general.php) and click on the subpage “AA RewriteRules“.
  4. You will then be presented with a page similar to the one below:


    Step 4

  5. Select all of the text in the big box “All WordPress RewriteRules” and copy it to the clipboard.
    (Left click on the box; CTRL + A [Select all]; CTRL + C [Copy])
  6. Go to my ‘WordPress RewriteRules to ISAPI RewriteRules Converter‘ page:


    Step 6

  7. Paste (CTRL+V) into the big box on the converter page, and click ‘Convert’.
  8. Select all of the converted rewrite rules returned by my utility and copy them into your ‘httpd.ini’ file (the ISAPI Rewrite Configuration file).
  9. Put the httpd.ini file in the root of your web directory, and assuming it’s enabled on the domain, your WordPress should now work with the Pretty Permalinks you setup.

Limitations

If you significantly change the structure of your site, you may have to go through this process again for some links to work on your blog. I, however, have never encountered this problem.

I hope you find this interesting/useful!

Pretty permalinks with ISAPI Rewrite all the way! ;-) Amen.