Custom Search

Thursday, January 24, 2008

Tutorial : User Login

First create the application.cfm file in your root folder,then write this code.

Application.cfm
 <cfapplication name="cfcentral"
    sessionmanagement="true"
    sessiontimeout="#CreateTimeSpan(0,0,30,0)#"
    />
  <cfset application.dsname="suman">
  <cfset request.dsname="suman">
  <cfset session.profile=StructNew()>
 
 <cfif IsDefined("Form.logout")>
  <cflogout>
 </cfif>
 
 <cflogin>
  <cfif NOT IsDefined("cflogin")>
   <cfinclude template="loginform.cfm">
   <cfabort>
  <cfelse>
   <cfif cflogin.name IS "" OR cflogin.password IS "">
    <cfoutput>
  <h2>You must enter text in both the User Name and Password fields.     </h2>
    </cfoutput>
    <cfinclude template="loginform.cfm">
    <cfabort>
   <cfelse>
    <cfquery name="loginQuery" dataSource="#request.dsname#">
    SELECT AdminName, Role
    FROM admin
    WHERE
  AdminName = '#cflogin.name#'
  AND Password = '#cflogin.password#'
    </cfquery>
    <cfif loginQuery.Role NEQ "">
  <cfloginuser name="#cflogin.name#" Password = "#cflogin.password#"
   roles="#loginQuery.Role#">
    <cfelse>
  <cfoutput>
   <H2>Your login information is not valid.<br>
   Please Try again</H2>
  </cfoutput> 
  <cfinclude template="loginform.cfm">
  <cfabort>
    </cfif>
   </cfif> 
  </cfif>
 </cflogin>
 
 <cfif GetAuthUser() NEQ "">
  <cfoutput>
   <form action="#CGI.script_name#?#CGI.query_string#" method="Post">
    <input type="submit" Name="Logout" value="Logout">
   </form>
  </cfoutput>
 </cfif>
   
Second create loginform.cfm file in your root folder.

loginform.cfm
 Please Log In
 
 <cfoutput>
    <form name="LoginForm" action="#CGI.script_name#?#CGI.query_string#" method="Post">
    <table>
    <tr>
    <td>username:</td>
    <td><input type="text" name="j_userName"></td>
    </tr>
    <tr>
    <td>password:</td>
    <td><input type="password" name="j_password"></td>
    </tr>
    </table>
    <br>
    <input type="submit" value="Log In">
    </form>
 </cfoutput>
   
Craete a table name admin with AdminName,Password,Role field.

WOW its done 

Tutorial : Pagination

hi,
here i introduce pagination technique.this very simple and esay.
this examples first part i only shows how you can make a data show page
with pagination.lets done...

create a page with name pagination.cfm,here the code bellow

pagination.cfm
 <cfparam name="request.dsname" default="suman">
 <cfparam name="TableName" default="admin">
 <cfparam name="MaxRow" default="5">
 <cfparam name="url.PageNumber" default="1">
 
 <cfif NOT IsDefined("session.qSelectQuery") OR IsDefined("url.CreateNewSession")>
  <cfquery name="session.qSelectQuery" datasource="#request.dsname#">
   SELECT *
   FROM #TableName#
  </cfquery>
 </cfif>
 
 <cftable query="session.qSelectQuery" startrow="#((url.PageNumber*MaxRow)-MaxRow)+1#" maxrows="#MaxRow#" colheaders="yes">
  <cfloop list="#session.qSelectQuery.ColumnList#" index="Column">
    <cfcol text="#Evaluate(Column)#" header="#Column#">
  </cfloop>
 </cftable>
 
 <cfoutput>
  <cfloop from="1" to="#Ceiling(session.qSelectQuery.RecordCount/MaxRow)#" index="i">
    <a href="#CGI.SCRIPT_NAME#?PageNumber=#i#">#i#</a>
  </cfloop>
  .........page #url.PageNumber# of #Ceiling(session.qSelectQuery.RecordCount/MaxRow)#......
  <a href="#CGI.SCRIPT_NAME#?CreateNewSession">run the select query again</a>
 </cfoutput>
   
change the parameter as you need.then check with browser.
what you see...yes its done.

for reusable pagination technique read the second part.

Tutorial : Pagination (Function based)

hi,
this is second part of pagination example.here i show pagination technique
that is reusable.at first i create a function for pagination then call it when need.
lets do the job.

create a page name pagination.cfm,code bellow.

pagination.cfm
 <cfoutput>#Pagination("Suman","admin")#</cfoutput>
 
 <cffunction name="Pagination" access="remote" returntype="any" output="yes">
  <cfargument name="dsname" required="yes">
  <cfargument name="TableName" required="yes">
  <cfargument name="MaxRow" default="10" required="no">
  
  <cfparam name="url.PageNumber" default="1">
  
  <cfif NOT IsDefined("session.qSelectQuery") OR IsDefined("url.CreateNewSession")>
   <cfquery name="session.qSelectQuery" datasource="#dsname#">
    SELECT *
    FROM #TableName#
   </cfquery>
  </cfif>
  
  <cftable query="session.qSelectQuery" startrow="#((url.PageNumber*MaxRow)-MaxRow)+1#" maxrows="#MaxRow#" colheaders="yes">
   <cfloop list="#session.qSelectQuery.ColumnList#" index="Column">
  <cfcol text="#Evaluate(Column)#" header="#Column#">
   </cfloop>
  </cftable>
  
  <cfoutput>
   <cfloop from="1" to="#Ceiling(session.qSelectQuery.RecordCount/MaxRow)#" index="i">
  <a href="#CGI.SCRIPT_NAME#?PageNumber=#i#">#i#</a>
   </cfloop>
   .........page #url.PageNumber# of #Ceiling(session.qSelectQuery.RecordCount/MaxRow)#......
   <a href="#CGI.SCRIPT_NAME#?CreateNewSession">run the select query again</a>
  </cfoutput>
 </cffunction>
   
the pagination function is done.you can use it in your page more than one
time without write huge code....

Tutorial : Pagination (Component based)

hi,
this is the final part of pagination example.here i use object oriented coldfusion.
this create a pagination object which you can call anywhere in your application.

here you need to create to file named pagination.cfc and pagination.cfm
for simpler issue you should place the both file in a common folder.
the code goes bellow.

pagination.cfc
 <cfcomponent author="cfsuman@gmail.com">
  <cffunction name="Pagination" access="remote" returntype="any" output="yes">
   <cfargument name="dsname" required="yes">
   <cfargument name="TableName" required="yes">
   <cfargument name="MaxRow" default="10" required="no">
   
   <cfparam name="url.PageNumber" default="1">
   
   <cfif NOT IsDefined("session.qSelectQuery") OR IsDefined("url.CreateNewSession")>
    <cfquery name="session.qSelectQuery" datasource="#dsname#">
  SELECT *
  FROM #TableName#
    </cfquery>
   </cfif>
   
   <cftable query="session.qSelectQuery" startrow="#((url.PageNumber*MaxRow)-MaxRow)+1#" maxrows="#MaxRow#" colheaders="yes">
    <cfloop list="#session.qSelectQuery.ColumnList#" index="Column">
   <cfcol text="#Evaluate(Column)#" header="#Column#">
    </cfloop>
   </cftable>
   
   <cfoutput>
    <cfloop from="1" to="#Ceiling(session.qSelectQuery.RecordCount/MaxRow)#" index="i">
   <a href="#CGI.SCRIPT_NAME#?PageNumber=#i#">#i#</a>
    </cfloop>
    .........page #url.PageNumber# of #Ceiling(session.qSelectQuery.RecordCount/MaxRow)#......
    <a href="#CGI.SCRIPT_NAME#?CreateNewSession">run the select query again</a>
   </cfoutput>
  </cffunction>
 </cfcomponent>
   
pagination.cfm
 <cfscript>
  PaginationObject=Createobject("Component","pagination");
 </cfscript>
 
 <cfset PaginationObject.Pagination("suman","admin")>
   
hei now run the pagination.cfm file in your browser.
whats hapend?yes this call object oriented web programming.

Tutorial : Application code documentation


Note: very recent i found little bug in this tutorial.
Soon i will fix it.
If you want then you may fix it.
This a challange for you...


hi,
here i show a object oriented technique for documented your code.
when you build your site then you may want a pdf file here you can get all of your code.for this purpose you can try it.

for this create two file in your application root folder named codedocumentation.cfc and codedocumentation.cfm,here the both file code.

codedocumentation.cfc
 <cfcomponent hint="Create Code Documentation" output="yes">
 
  <cffunction name="GetApplicationMap" access="remote" returntype="array">
 
   <cfargument name="SiteRootDirectory" type="string" required="yes">  
   <cfargument name="OutputFileFormat" type="string" required="yes">    
  
   <cfset FolderExploreStatus=0>
 
   <!--- Create a Array for hold all  Files List--->  
   <cfset FileArray=Arraynew(2)>
 
   <!---Root Directory's Contents--->
   <cfdirectory action="list"
    directory="#arguments.SiteRootDirectory#"
    name="ParentDirectory"
    >
 
   <!---Root Directory's Directory--->
   <cfquery name="DirectoryList" dbtype="query">
    SELECT *
    FROM ParentDirectory
    WHERE TYPE='Dir'
   </cfquery>
   
   <!---List of all Folder Path--->
   <cfset FolderPathArray=Arraynew(1)>
   <cfset TempFolderPathArray=ArrayAppend(FolderPathArray,arguments.SiteRootDirectory)>
   <!---<cfdump var="#FolderPathArray#"><br/>--->
   
 
   <!--- Create a Array for hold  Folder List--->  
   <cfset FolderArray=ArrayNew(2)>
 
   <cfset FolderCounter=1>
   <cfloop query="DirectoryList">
    <cfset FolderArray[FolderCounter][1]=DIRECTORY>
    <cfset FolderArray[FolderCounter][2]=NAME>
    
    <cfset FolderPath="#DIRECTORY#\#NAME#\">
    <!---<cfdump var="#FolderPath#"><br/>--->
    <cfset TempFolderPathArray=ArrayAppend(FolderPathArray,FolderPath)>
    <cfset FolderCounter=FolderCounter+1>
   </cfloop>
 
   <cfset FolderNumber=ArrayLen(FolderArray)>
  
   <cfif FolderNumber GT 0>
    <cfloop condition=" FolderExploreStatus EQ 0 ">
  <cfset CurrentFolderDirectory=FolderArray[1][1]>
  <cfset CurrentFolderName=FolderArray[1][2]>
  
  <cfset CurrentFolderPath="#CurrentFolderDirectory#\#CurrentFolderName#\">
  
  <!--- List the Current directory's Directory--->  
  <cfdirectory
   action="list"
   name="ChildDirectory"
   directory="#CurrentFolderPath#"
   > 
 
  <!--- List the Current directory's Directory--->  
  <cfquery name="ChildDirectoryList" dbtype="query">    
   SELECT *
   FROM ChildDirectory
   WHERE TYPE='Dir'
  </cfquery>
    
  <cfset TempFolderArrayDelete=ArrayDeleteAt(FolderArray,1)>
    
  <cfset FolderCounter=ArrayLen(FolderArray)>
  <cfset FolderCounter=FolderCounter+1>
   
  <cfloop query="ChildDirectoryList">
   <cfset FolderArray[FolderCounter][1]=DIRECTORY>
   <cfset FolderArray[FolderCounter][2]=NAME>  
   
   <cfset FolderPath="#DIRECTORY#\#NAME#\">
   <!---<cfdump var="#FolderPath#"><br/>--->
   <cfset TempFolderPathArray=ArrayAppend(FolderPathArray,FolderPath)>
   <cfset FolderCounter=FolderCounter+1>
  </cfloop>
 
  <cfset FolderNumber=ArrayLen(FolderArray)>
  <cfif FolderNumber LT 1>
   <cfset FolderExploreStatus=1>
  </cfif>
  
    </cfloop>
   </cfif>
   
   <!---Now we find final folder path array--->
   
   <cfset TotalFolder=ArrayLen(FolderPathArray)>
   
   <cfloop from="1" to="#TotalFolder#" index="index">
   
    <!---<cfoutput>#FolderPathArray[index]#</cfoutput><br/>--->
    <!---Root Directory's .cfm FileList--->
    <cfdirectory action="list"
   filter="*.cfm"
  directory="#FolderPathArray[index]#"
  name="CFMFileList"
  >
  
    <!---Root Directory's .cfml FileList--->
    <cfdirectory action="list"
   filter="*.cfml"
  directory="#FolderPathArray[index]#"
  name="CFMLFileList"
  >
  
    <!---Root Directory's .cfc FileList--->
    <cfdirectory action="list"
   filter="*.cfc"
  directory="#FolderPathArray[index]#"
  name="CFCFileList"
  >
  
    <!---Root Directory's .htm FileList--->
    <cfdirectory action="list"
   filter="*.htm"
  directory="#FolderPathArray[index]#"
  name="HTMFileList"
  >
  
    <!---Root Directory's .html FileList--->
    <cfdirectory action="list"
   filter="*.html"
  directory="#FolderPathArray[index]#"
  name="HTMLFileList"
  >
  
    <!---Root Directory's .css FileList--->
    <cfdirectory action="list"
   filter="*.css"
  directory="#FolderPathArray[index]#"
  name="CSSFileList"
  >
  
    <!---Root Directory's .js FileList--->   
    <cfdirectory action="list"
   filter="*.js"
  directory="#FolderPathArray[index]#"
  name="JSFileList"
  >
  
    <!---Root Directory's All FileList--->  
    <cfquery name="FileList" dbtype="query">
  SELECT *
  FROM CFMFileList
  UNION
  
  SELECT *
  FROM CFMLFileList
  UNION
  
  SELECT *
  FROM CFCFileList
  UNION
  
  SELECT *
  FROM HTMFileList
  UNION
  
  SELECT *
  FROM HTMLFileList
  UNION
  
  SELECT *
  FROM CSSFileList
  UNION
  
  SELECT *
  FROM JSFileList
    </cfquery>
  
    <cfset FileCounter=ArrayLen(FileArray)>
    <cfif FileCounter EQ 0>
  <cfset FileCounter=1>
    </cfif>
    <cfloop query="FileList">
   <cfset FileArray[FileCounter][1]=NAME>
   <cfset FilePath="#DIRECTORY#\#NAME#">
   <cfset FileArray[FileCounter][2]=FilePath>
   
  <cfset FileCounter=FileCounter+1>
    </cfloop>
   
   </cfloop>
   
   <!---Now we find final file array--->
   <cfdump var="#FileArray#">
   
   <cfset TotalFile=ArrayLen(FileArray)>
   
   <cfif TotalFile GT 0>
    <cfdocument format="#arguments.OutputFileFormat#">
  <cfdocumentsection>
   <cfdocumentitem type="footer">
    <font size="-3">Page #cfdocument.currentpagenumber#</font>
   </cfdocumentitem>
     
   <cfloop from="1" to="#TotalFile#" index="position">
 
    <cfdocumentitem type="header">
     <cfoutput>
   <font size="-3">
    <i>
     Code Documentation
    </i>
   </font>
     </cfoutput>
    </cfdocumentitem>
 
    <cffile
     action="read"
     file="#FileArray[position][2]#"
     variable="FileCode"
     >
    <cfoutput>
     <h3>
   <i>
    #FileArray[position][2]#
   </i>
     </h3>
     <br/>
 
     <h2>
   #FileArray[position][1]#
     </h2>
    </cfoutput>
    <br/>
    
    <cfoutput>
     #HTMLCodeFormat(wrap(FileCode,75))#
    </cfoutput>
   </cfloop>
    </cfdocumentsection>
    </cfdocument> 
   </cfif>
 
   <cfreturn FolderPathArray>
  </cffunction>
 </cfcomponent>
   
codedocumentation.cfm
 <cfset ApplicationRootpath=ExpandPath(".")>
 
 <cfif IsDefined("Form.GetMap")>
  <cfscript>
   ApplicationMapObject=CreateObject("Component","codedocumentation");
  </cfscript>
  <cfset ApplicationMapObject.GetApplicationMap(Form.ApplicationRootpath,Form.FileFormat)>
 </cfif>
 
 <div align="center">
  <cfform name="FileFormatForm" action="#CGI.SCRIPT_NAME#" method="post" target="_blank">
   <cfselect name="FileFormat" label="Output Format">
    <option value="pdf">PDF</option>
    <option value="flashpaper">Flash Paper</option>
   </cfselect>
   <cfinput type="hidden" name="ApplicationRootpath" value="#ApplicationRootpath#">
   <cfinput type="submit" name="GetMap" value="Get Application Map">
  </cfform>
 
 
 </div>
   
hei..
now run the codedocumentation.cfm file in your browser.
submit the form.
what you see...?
yes its object oriented web programming.

Tutorial : cfsavecontent

Copy the below code and paste your editor.Before it you
need two table one tblCountry  with CountryID,CountryName
Column And another tblCounty with CountyID,CountryID,
CountyName Column.Edit code if needed.

<cfquery name="qCountry" datasource="#DataSourceName#">
SELECT * FROM tblCountry
</cfquery>


<cfquery name="qCounty" datasource="#DataSourceName#">
SELECT     CountyID, CountryID,CountyName
FROM         tblCounty
</cfquery>


<cfsavecontent variable="CountyList">

var CountyName = [];

var SelectedCountry=Country.selectedItem.data;


<cfoutput query="qCounty">

var CountryID=#CountryID#;

if (CountryID == SelectedCountry)

{

var AddCounty = {label:'#CountyName#', data:#CountyID#};

CountyName.push(AddCounty);

}

</cfoutput>



County.dataProvider = CountyName;

</cfsavecontent>


<div align="center">
<cfform format="flash" width="300" height="200">

<cfformgroup type="panel" label="Select Country and County">

<cfselect label="Country" name="Country" query="qCountry" value="CountryID" display="CountryName" required="yes" onchange="#CountyList#">

</cfselect>

<cfselect name="County" label="County">

Select

</cfselect>

</cfformgroup>

</cfform>
</div>