PHP Magic Constants __LINE__

November 30, 2006

Going through the php document I came across magic constant __LINE__.

-What this contants does?
returns the current Line No. of the file.

-How we can use this constant in development?
I started using this function for most of the debugging purposes. I simply append it with my echo messages. So when I need to go to code, I can directly go the same line.

I more magical constant I used along with __LINE__ is __FILE__. __FILE__ gives the name of the current script file name.

These two functions reduced 20% of my debugging time.


Application.cfm AND Application.cfc not working in similar manner.

November 28, 2006

I am working on the ColdfusionMX7 and Application.cfm is not working properly, I defined application variable and tried to output it on cfm page its giving me error

Element PAGENAME is undefined in APPLICATION.

My Application.cfm look like

<cfapplication name=”TestApp1″ clientManagement=”yes” sessionmanagement=”yes”>
<cfset Application.pageName=”foo”>
</cfapplication>

Where as Application.cfc is working well, its look like

<cfcomponent displayname=”">
<cfset this.name=”testApp1″>
<cffunction name=”onapplicationStart” returntype=”boolean” output=”false”>
<cfset Application.pageName=”Application Inside page”>
<cfreturn true>
</cffunction>
</cfcomponent>

What difference in between them? Any ideas


Php Framework CRUD model

November 27, 2006

Its time for framework, something which offers ready code for most common things we do for every new project. There are numbers of php frameworks available online which allows rapid application development. Also most of the frameworks varies from their config style. Learning curve is again involved in learning these new framework. In many cases I found its quite difficult to customize the frameworks. Every framework has their own style of code which you might not be familiar with.

Then what to do, I thought how about writing my own framework. Some points which basic CRUD model should have are :

-Ready Classes structure
-Code formatting
-Form generation
-Validation
-Easy to understand by any other developer
-Should allow generation of Create, Read, Update and Delete in short CRUD

I have started thinking on the same line. I gone through old codes and started observing the common structure.
-in most of my projects I found all the classe has common structure
-common methods in each class eg. getByID, getAll, addUpdate
-inclusion of file is of same pattern
-most of the php code were common.

After identifying all these points, I started buidling my own framework. You believe me or not, my very basic framework was ready in just 2 hours.

I enjoyed building my own framework.

I will share my experience of building it next time.


Coldfusion trick we did to find values in array

November 24, 2006

In coldfusion I didnt find function to find values within array. Requirement was to look wheather specific value is present or not. Such function is present in PHP.

I look for various implmentation and I though structure could solve this issues. And magic logic works. Let me explain you what we did

-We received array, we converted it to the structure.
-Each value from the array is now treated as key in structure
-At the end we used structKeyExists function.

This way we achieved the functionality that we need.


Coldfusion redirect to different url

November 24, 2006

While developing application, many times I found the need of redirecting your current page. Coldfusion has a nice cflocation tag which allow you load/redirect new page in the browser.

In between your script, when cflocation tag arrives, coldfusion simply stops the execution of the current page and opens a new page provided as url to cflocation tag.


Javascript CSS based error pop up

November 24, 2006

I have created a javascript which I will be extending to use as library.
-It basically identify if error is occured
-Based on that I pops up a div with Error message
-It dynamically finds the position of form element to Pop up the error message.

I dont like javascript pop up which blocks my entire operation. It many time annoying to user.

I am still working on it. While working on it I faced a problem with different browser mainly with IE and Mozilla.

I found some script on the net which finds the exact position based on the offsetParent property of any element.

I am trying to use functionality to provide the tooltip kinda of look and feel

I will explain the code, once I am done with the coding. I will upload it for review and later on publish it.


Ternary Operator implemenation in PHP and Coldfusion

November 22, 2006

What is Ternary Operator ?

t’s a shortcut for an if-else statement, and is also known as a conditional operator. Its operate on the three input

Ternary operators save time and lines of code. Its more preferable when you are performing simple conditional operation.

In PHP it works as:

“boolean_condition?true_value:false_value”

In coldfusion it works as:

There is not any direct implementation of operator, but coldfusion uses function IIf for the same purpose

Syntax : IIf(condition, string_expression1, string_expression2)

Without Ternary Operator

var a=1,c=”";
if(a==1)
{
c=”Hello”;
}
else
{
c=”Bye”;
}

With Ternary Operator

var a=1,c=”";
c=(a==1)?”Hello”:”Bye”;


Steps to make an in place editing in HTML using Javascript

November 20, 2006

1) Create div container where you want in place editing

2) Give it some id. eg
<div id=”d”> Here is the text that will get changed to input field</div>

3) Onclick of div call javascript function which will hold the innerHTML value of div.
<div id=”d” onclick=”edit();”> Here is the text that will get changed to input field</div>

4) form a string to print the input field in function
var textValue = ‘<input type=”text” id=”editedValue” value=”‘+ innerHTML +’” onblur=”save()”>’

5) Onblur of input field, get the values entered in textField.
var editedValue = document.getElementById(“editedValue”).innerHTML;

6) Assign it back the the original container
document.getElementById(“d”).innerHTML = editedValue;

Your in place editing is ready, fancy stuff

Pseudo Code

//call edit on click of div
function edit()
{
var textValue = document.getElementById(“d”).innerHTML;
document.getElementById(“d”).innerHTML=’<input type=”text” id=”
editedValue” value=’+'” ‘+textValue+’”>’;
}

//call save on blur of textField
function save()
{
var
editedValue = document.getElementById(“editedValue“).value;
document.getElementById(“d”).innerHTML=
editedValue;
}

I will put the working sample tomorrow.


Recursive function in php, useful learning for today

November 20, 2006

I worked on the recursive function today to create cfdump which give information about the object/array/structure in very neat and clean format. While applying recursive function to array in my implmentation, I was not able to show the nested levels of the elements in array.

Here is my array which I want to print

$tempArray=array(“info”=>array(“firstName”=>”vijay”,”lastName”=>”khambalkar”,”age”=>array(20,30,40)), “address”=>array(“Borivali”,”west”),”test”=>”just a test element”);

Output I was trying to print is

–vijay
–khambalkar
––20
––30
––40
–Borivali
–west
just a test element

I initial thought on using global varible which keeps track of the nested level. I thought over it and decide before each recursive call I will increment my variable, but in this case every time the global varible was just incrementing. As after coming to original level we need to decrement the global variable. So here is my final script which allowed me to print the expected output.

$tempArray=array(“info”=>array(“firstName”=>”vijay”,”lastName”=>”khambalkar”,”age”=>array(20,30,40)), “address”=>array(“Borivali”,”west”),”test”=>”just a test element”);

function showList($tempArray)
{
static $counter = 0; //Static varible count is defined to keep the track of the counter globally.

foreach($tempArray as $key=>$value)
{

if(is_array($value) || is_object($value))
{

$counter++; //This variable is incrementing each time functions get called recursively
showList($value);
$counter–; //When function is back, it simply decrement the value

}
else
{

echo str_repeat(“–”, $counter) . “$value <br>”; //This statement repeating the supplied character supplied by counter

}
}
}
showList($tempArray);

In my initial script counter was missing.

Overall good learning in recursive function. Although cfdump is not yet ready. Will do it some day later in this week


Flex StartDrag and StopDrag

November 18, 2006

I helped one of my team member to solve the problem of Drag, Drop on the component. We tried with traditional method
-On mouseDown get reference of object
-On mouseMove change x and y coordinates
-On mouseUp remove listener

But this methods was too slow. i.e It was not changing the object location consistently. After searching little bit we found new methods startDrag and stopDrag.

Calling this method on the object solve the problem of inconsitence that we faced with traditional method. But we were not able to control the x,y coordinates for the object.

Good to see this greate features of drag and drop with startDrag and stopDrag functions.