Pages

Wednesday, August 10, 2011

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/

Sunday, June 26, 2011

Transactions Shortcut in Cakephp

Add the following functions in app/models/AppModel

function begin()
{
$db=&ConnectionManger::getDataSource
$db->begin($this);
$this->useDbConfig;
}

function commit()
{
$db->commit($this);
}

function rollback()
{
$db->rollback($this);
}

Now in you controller
Use
$this->Model->Begin()
$this->Model->commit();
$this->Model->rollback();

Cakephp:To run an ajax on page load use

$this->Ajax->remoteFunction($options);

where $options are 'url'=>controller,action
'update'=>
'with'=>Form.SearlizeElements(new array($("BillClientId")))');

Using Pear Framework in Cakephp

1. Copy the pear in the venders directory in the root(there are 2 one in app folder as well)

If you wish to use that one copy cake/config/paths to app/config/paths and change the following line

/**
* Path to the vendors directory.
*/
if (!defined('VENDORS')) {
define('VENDORS', CAKE_CORE_INCLUDE_PATH.DS.app.DS.'vendors'.DS);
}

After copying Pear now you have the framework its time to copy components

2.copy the package needed in the pear directory

Now we will create a path for our Pear package so that Cake can easily access that

For that copy cake/config/paths.php to app/config.paths.php add an entry after venders definition as follows

"define ('PEAR',VENDORS.'Pear'.DS)";

This defines a constant for PEAR path...

Now we need to set that path in our include path for that in your boot_strap file mark an entry as

ini_set("include_path",PEAR.PATH_SEPARATOR.ini_get("include_path"));

so path is now set in "include_path".

Next we need our controller to recognize this so just before start of controller or view(as per requirement)enter the following line

App::import('Vendor','Words',array('file'=>'../vendors/Pear/Numbers/Words.php'));

That's all now you can create an object of class and use it as follows

$numberconverter=new number_words();

echo $numberconverter->towords($grandtotal);

If your view is going blank on creation of object check that the import link on top of controller or view is correct.

Cakephp:send multople elements values in observe field

$options=>array('url'=>array('controller'=>'admin','action'=>'update_list'),'update'=>'divIdToUpdate','frequency'=>'0.2','with'=>'Form.serialize elements($('elementid1','elementid2,elementid3')));

echo $this->Ajax->observefield('Schedulepart',$options);

Cakephp:Image on clear Button

$this->Html->link($this->Html->image('button-clear.gif',array('alt'=>('clear',true),'border'=>'0'),array('onclick'=>"document.form.reset();return false;",'escape'=>'false)

Cakephp:Selecting Multiple Entries from a list

use the following code

$this->Form->select('Model.name',$listarray,'selectedvalue',array('multiple'=>'multiple','size'=>'5');

CakePHP:Button Image does not appear

Button Image does not appears in fact html tags are converted to escape sequences

When ever an button image is inserted tags appear as in written instead of showing image

Sunday, May 1, 2011

Fatal error: Maximum execution time of 30 seconds exceeded in

Fatal error: Maximum execution time of 30 seconds exceeded in
Faced this error while installing a PHP project on my webserver.The solution to This problem is simple Simply increase the Execution timeout in the Php.ini file till the time you are getting the error.

Search for these lines

; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 150

Bingo here you go