Pages

Thursday, August 11, 2011

Javascript to check if variable exists

if(typeof(variablename)= = = 'undefined')
{
//do action
}

Ajax Request Using prototype.js

Prototype a Javascript framework makes life of Ajax Request as easier as ever though they are also available in Jquery but I prefer this framework infact I heard somewhere that Jquery is also based on this

All you have to do is download and include prototype.js from
http://www.prototypejs.org/

and then use the following Url
new Ajax.Request("url",
{
asynchrnous:"false";
method:post;
onSuccess: responsefunction;//(function name to which response needs to be sent)
parameters:{sum:amount,rowid:rowid},
onComplete:function(request,json){ element.hide('loader');},
onLoad:Element.show('loader')
});

function responsefunction(rsp)
{
var response =eval('(' + rsp.responseText + ')')
console.log(response)//show response in firebug
var amt=rsp.request.parameter.sum;
var name=response.Name //array which came from the json data thrown by url
}

);
//since its javascript data outputted must be json always by the url.
//rsp contains the values of all your response which comes from url

//at the url
json_encode($fetchdata);//most important to encode your data be it any file
?>

this $fetchdata comes as json in the responsefunction(rsp)

Using Xml Http Response Object

var xmlhttp
if(window.XMLHttpRequest)
{
//for IE7+,firefox,opera,safari
xmlhttp=new xmlHttp Request();
}
else
{
xmlhttp=new ActiveXobject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readystate=4 && xmlhttp.status=200)
{
//do the task from data
doc.getElementById("load1").style.display="none";
}
else
{
//do waiting task
}
xmlhttp.open("Post",'url',true);
xmlhttp.send();
}

Dynmically creating a table using Javascript and adding rows to it

For this you need to first of all define an empty html table element with an id property at least to refer to it my dummy was.
table
tr
th /th
th /th
/tr
tr
td /td
td /td
/tr
/table

Written without tags so that it is not interpreted by blog as html.

My table has a header row and a normal row

The table rows starts from index 0.You can use following code from a js file or directly on an html file.
(I recommend js file helps in re usability of code)

var tbl=document.getElementByid('tbl');
//Get the last row

var lastrow=tbl.rows.length

//since I already have a header row in my table and a row manually added so in my case //new row will be equal to last row (table length does not includes the last row)

var iteration=lastrow;
//create a new row
row=tbl.insertRow(lastrow);

//leftcell
//general text node to show the Sno's
var cellleft=row.insertcell(0);
var textnode=document.CreatetextNode(value of the node);
cellleft.appendChild(textnode);

//rightcell
//creation of a textbox node
var cellright=row.insertCell(1);
var el=document.CreateElement('input');
el.type='text';
el.name='name';
el.id='id';
cellright.appendChild(el);

you can add more columns by incrementing index in var cellright=row.insertCell(1);

One of my columns required to create a list dynamically..since I need the whole list and so I cloned the same from my first row(it was the only need for which I had to create a row manually)

To clone an element use the following
elsource=document.getElementById('list element');
eldestination=elsource.clone();
eldestination.name='new list name'
eldestination.id='new id'

So that's how we can dynamically add rows to a table....

Wednesday, August 10, 2011

Element.dispatchEvent(event) is not a function

Occurs when we use jquery and prototype framework at the same time.These 2 frameworks conflict
use the following code

$$=jquery.noConflict()

and then in each jquery code use $$ instead of $

Class 'Database_config' not found during Baking

Use
Cake Bake -app path to your app and issue resolved....

btw I have just tested this process looks a great automated process to me..will add more on it soon

Getting Previous or Next Dates in PHP

date('Y-m-d',strtotime($date +1 day));
date('Y-m-d',strtotime($date +1 month));
date('Y-m-d',strtotime($date +1 year));

Scheduling Crone Jobs from Console shells in Cakephp

I wrote a scheduler and then used the file given at
http://book.cakephp.org/view/1110/Running-Shells-as-cronjobs
to run the same as crone jobs but it wasn't that easy to proceed.

Usually webspace providers provide services to schedule crone jobs and get their output via mail from their control panel.Since my service provider provides service from a linux server so following tutorial is tried and tested on linux server however I will be glad if someone pastes their experience with windows machines as well...

So lets start from beginning I will discuss the errors faced as we proceed

1) I created a file cakeshell and copied it to vendors folder in root when I tried to run my shell I got following errors

/bin/bash^M: interpreter:No such file or directory
Solution:Remove the commented line at the top of the file cakeshell i.e

#! /bin/bash

2) Generally web space providers add php path to the system path already so in those cases we do not need -cli /usr/bin to add as parameter as given in the link however if it is not added you will need to add it.

If you are unable to find the path i.e you are getting error msg such as file not found make sure you are starting from root

Mine was as follows

/home/username/public_html/app name/vendors/cakeshell
-console /home/username/public_html/app name/app/console
-app /home/username/public_html/app name/app
-cli /usr/bin (if your php path is not added to system path else not required)

3)Make sure cakeshell and cake(in console) have permissions to execute for all

That's all create a shell and add a crone job to your scheduler on server and it will run

Database errors while running cakephp console

Failed to open stream No such file or directory

One way is to use -app /cake/app/ and then run your cakephp shell

for example if your shell name is myshell use

cake myshell -app /cake/app/(absolute/relative path to app folder)

other way round is to copy console folder in your app directory in that case simply run

cake myshell and it will work

Cannot Modify header information-headers already sent by (output started at)

Issue looked a bit awkward at first but the solution was simple

Check for spaces before and after the opening and closing tags respectively in the concerned file

PHP Convert a date to Mysql Format

For PHP 5.3.5 this works(worked on my development environment)

$orgdate=DateTime::CreatefromFormat('d-m-Y',$orignaldate);

$converteddate=$orgdate->format('Y-m-d');

In case of 5.2.4 I used the Cakephp Helper to convert the same so check your framework you might have a helper for the same for core PHP you might need to play with Date function

Here is how it went in case of cakephp

Add this line to the top of your view/controller

App::import('Helper','Time');

$time=new TimeHelper //create object of class

$data['date']='28-07-2011';

$time->format('Y-m-d',$data['date']);

worked on hosting env i.e php 5.2.4

Check in Cakephp if Flash Message has been set or not

Here is the code which helps to check if flash message has been set or not..sometimes its a need to use a default message if it is not set..

if(!($this->Session->check('Message.flash'))));

Generate Excel from PHP using Code

Many a times excel needs to be generated.People usually generate the same using screenshot method i.e first they output the text to the screen and then they map the same to Excel file which may include invalid characters many a times recently I found a good link to generate Excel using code...I successfully used it in Cakephp so I believe its also good on frameworks here is the link to the same

http://smartcoderszone.com/2010/09/how-to-generate-excel-report-with-php-and-mysql/