Thursday, June 4, 2009

How to display currency symbol using coldfusion 8, Cfgrid fomat HTML

Question - "how to display the currency $ in cfgrid while using cfgridcolumn, the format is HTML"

There are two solutions to the above question

1. Have the SQL format the data and return the column values with $ symbol added to it.

2. The below code I found on internet, but it is the only solution that I could found if you are using cfgrid and format is HTML, I am posting it so that it is available.

<--- Query --->
<cfscript>
paymentQuery = queryNew("fname,lname,amount", "varchar,varchar,double");
queryAddRow(paymentQuery);
querySetCell(paymentQuery, "fname", "John");
querySetCell(paymentQuery, "lname", "Smith");
querySetCell(paymentQuery, "amount", "103.846");
queryAddRow(paymentQuery);
querySetCell(paymentQuery, "fname", "Mary");
querySetCell(paymentQuery, "lname", "James");
querySetCell(paymentQuery, "amount", "200");
queryAddRow(paymentQuery);
querySetCell(paymentQuery, "fname", "Peter");
querySetCell(paymentQuery, "lname", "Stone");
querySetCell(paymentQuery, "amount", "3.25");
</cfscript>
<--- Cfform and HTML grid --->
<cfform>

<cfgrid name = "paymentGrid" format="html">
<cfgridcolumn name = "fname" header = "First name">
<cfgridcolumn name = "lname" header = "Last name">
<cfgridcolumn name = "amount" header = "amount" dataalign="right">
<cfloop query="paymentQuery">
<cfgridrow data ="#paymentQuery.fname#, #paymentQuery.lname#, #lscurrencyformat(paymentQuery.amount,'local','English (US)')#">
</cfloop>
</cfgrid>

</cfform>

Note -- the above will work with COLDFUSION 8, to work with CF MX 7 you need to change the
lscurrencyformat function(just remove the last two variables).

Wednesday, June 3, 2009

How to restart ColdFusion from Command prompt in LINUX

You can restart Coldfusion / Jboss / Mysql and other services using the following commant

Coldfusion

/etc/init.d/coldfusion_8 restart or /etc/init.d/coldfusion restart

Jboss

/etc/init.d/ jboss restart

and MySql

/etc/init.d/mysqld restart

Tuesday, June 2, 2009

Getting Twitter Followers using Coldfusion

Question :
How to get the number of followers on Twitter using Coldfusion ?


Well, just use cfhttp and some string functions to get the result, I have created a function for the same which can be used, the function accepts the twitter url.

<cffunction name="twitter" access="public" returntype="any" output="no">
<cfargument name="url" required="yes" type="string">
<cfhttp
url="#url#"
method="get"
resolveurl="yes"
useragent="Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)">
</cfhttp>
<cfset stringTOCheck = '<span id="follower_count" class="stats_count numeric">'>
<cfset lenghtOfCheck = len(stringTOCheck)>
<cfset firstOccurance = Find('#stringTOCheck#','#cfhttp.FileContent#')>
<cfset secondOccurance = Find('</span>','#cfhttp.FileContent#','#firstOccurance#')>
<cfset startFrom = #firstOccurance#+#lenghtOfCheck#>
<cfset endFrom = #secondOccurance#-(#firstOccurance#+#lenghtOfCheck#)>
<cfset followers = MID('#cfhttp.FileContent#',#startFrom#,#endFrom#)>
<cfreturn followers>
</cffunction>

Enjoy !

Monday, June 1, 2009

Adding new data to XML using Coldfusion

Recently asked a Question 

"Need to create an XML file in which data is appended to existing XML"

Answer is pretty simple, first create  XML using cfxml tag

<cfprocessingdirective suppresswhitespace="Yes">
<cfxml variable="xmlobject">
<data>
        <name>Brijesh Chauhan</name>
        <email>abc@gmail.com</email>
        <name>Radhika Chauhan</name>
        <email>def@gmail.com</email>
</data>
</cfxml>

Then insert New XML nodes using array insert, we will always insert the new data at first and second position(as in this case there are two children's).

<cfscript>
ArrayInsertAt(xmlobject.data.XmlChildren, 1, XmlElemNew(xmlobject,"name"));
ArrayInsertAt(xmlobject.data.XmlChildren, 2, XmlElemNew(xmlobject,"email"));
</cfscript>

Finally assign new data to the childrens

<cfset xmlobject.data.XmlChildren[1].XmlText = "Avyukth Chauhan"> 
<cfset xmlobject.data.XmlChildren[2].XmlText = "ghi@gmail.com"> 
</cfprocessingdirective > 

Simple !!!!

Interview Questions - Coldfusion, SQL and Linked Lists

Sharing some of the Interview Questions

Linked Lists 

Q1. How will you that a circular linked list has Loop.
A1. This is a very common question, take two pointers say p and q, p = q-1, and then traverse the list, if at any point during traversing p=q, then that is the Loop.

Q2. Given a linked list with n nodes where n is larger number and Even. How will you divide the linked list into 2 equal parts in one traverse.
A2. Take 2 pointers p and q, what we know is that for last node -> next = null, we are looking at the following equation, say when p = n (end node) , then q = n/2, which gives us p=2q, which means that move pointer p twice pointer q, that is shift pointer p by 2 nodes and pointer q by 1 node, that way when p = n, q would be n/2 and we have achieved the result.
HINT - hint for the above question is "how to find the (n-1)th node in the linked list.

SQL 

Q1. Given a database, with no choice of auto_increments, how will you select the next identity?
A2. The Answer varies to what database you are using.
a)MySql  - use the function "last_insert_id()" , your query should say "select last_insert_id() As NewID from table".
b) Sql Server - User @@Identity, your query would be "select @@Identity As NewID from table".
c)Oracle - use SEQUENCES, first you need to create a sequence "create sequence newId startwith 1 increment by 1" then use "newId.currentval" and "newId.nextval" to get the current and next values.