Tuesday, May 27, 2014

Phonegap and jQuery

We have developed an iPhone app using phonegap and jquery. As expected, we have performance issue. Our app is a one html page app and everything is being manipulated by javascript using jquery. It seems that binding an element to an event is the one causing the lag on the app. I have added an unbind first before I bind the elements to an event to clear previous binds. This seems to work.

Here is a sample of unbind code:
$("#mylink").unbind('click'); // add this code first
$("#mylink").click(function(){
    // code here
});

Hope this can help you speed up your phonegap apps with jquery.

Tuesday, February 8, 2011

How To Customize Main Layout of Error Pages in cakePHP

To customize the contents of each cakePHP errors, you need to create the following pages in this folder /app/views/errors/
- error404.ctp
- missing_action.ctp
- missing_component_class.ctp
- missing_component_file.ctp
- missing_connection.ctp
- missing_controller.ctp
- missing_helper_class.ctp
- missing_helper_file.ctp
- missing_layout.ctp
- missing_model.ctp
- missing_scaffolddb.ctp
- missing_table.ctp
- missing_view.ctp
- private_action.ctp
- scaffold_error.ctp


But cakePHP is using /views/layouts/default.ctp as layout for these error pages like 404, missing controller, etc. To customize it, you need to create a file /app/app_error.php with the following contents:
class AppError extends ErrorHandler {
 
 function _outputMessage($template) {
  $this->controller->layout = 'error_template'; // /app/views/layouts/error_template.ctp
  parent::_outputMessage($template);
 }

}

And create a template file "error_template.ctp" in /app/views/layouts.

Hope this one helps.

Monday, October 4, 2010

Multiple DB in cakePHP

In order to dynamically select DB during runtime in cakePHP is try to do it in Controller or in Model.

To do it in model:

class User extends AppModel {
var $name = 'User';
var $useDbConfig = 'general_syst';

function __construct($id = false, $table = null, $ds = null) {
$this->useDbConfig = 'db1';

parent::__construct($id, $table, $ds);
}

// your code here
// ....
}


When in controller

class Users extends AppController {

function action1(){
$this->User->useDbConfig = 'db1';

$users = $this->User->find('all');

// your code here
// ....
}

}


Hope this one helps.

Tuesday, November 24, 2009

Cannot send session cache limiter

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

If you encounter the above problem, try to set the encoding from UTF-8. UTF-8 encoidng add some characters before <?php

Tuesday, June 30, 2009

Access SimpleXMLElement Object Attributes

PHP simplexml_load_string is a simple way to extract xml data into an object. If you have the following result from calling

$obj = simplexml_load_string($xml);


SimpleXMLElement Object
(
[@attributes] => Array
(
[status] => success
)

[auth] => SimpleXMLElement Object
(
[token] => dxoYGmwncmntH07jJGro5h0rxNfE2Ni6
[seq] => 33
)

)


you can access status by

$obj->attributes()->status

Saturday, April 18, 2009

CakePHP 1.2 Versatile Model find('list')

Have you wanted to generate a list of options for your select but wanted the display field to be generated dynamically instead of using cakePHP find('list')?

This is the case.


MODEL
User hasOne Info

User
id
username
password
manager_id
Info
id
first_name
last_name
email
sex


I wanted to generate a list of all Users displaying the first_name, last_name and sex. And I wanted the indexes to be generated with User.id and the display field with this format "Info.last_name, Info.first_name (Info.sex)". I have developed a function to override cakePHP $model->find.

In your app_model.php, you include this function:

class AppModel extends Model {

function find($type, $options = array()) {
switch ($type) {
case 'superlist':
if(!isset($options['format'])) {
$options['format'] = '';
for ($i=0; $i $options['format'] .= "%$i ";
}
$options['format'] = trim($options['format']);
}

if (isset($options['index'])){
preg_match('/(.*?)\.(.*)/', $options['index'], $match);
$index_alias = $match[1];
$index_field = $match[2];
}else{
$index_alias = $this->alias;
$index_field = 'id';
}

$options['fields'] = array_merge(array("{$index_alias}.{$index_field}"), $options['fields']);
$list = $this->find('all', $options);

$result = array();
foreach ($list as $row){
$result[$row[$index_alias][$index_field]] = $options['format'];

foreach ($options['fields'] as $index=>$field){
preg_match('/(.*?)\.(.*)/', $field, $match);
$field_alias = $match[1];
$field_field = $match[2];

$result[$row[$index_alias][$index_field]] =
str_replace('%'.$index, $row[$field_alias][$field_field],
$result[$row[$index_alias][$index_field]]);
}
}

return $result;
break;

default:
return parent::find($type, $options);
break;
}
}

}


This is how you call the generate list function in your controller action.

// if you have not declared hasOne Info in User model, this is needed in order for User model generate Info model with it and you can pass condition on Info fields
$this->User->bindModel(
array(
'hasOne' => array('Info')
)
);

$users = $this->User->find('superlist',
array(
'format' => '%3, %2 (%1)',
'index' => 'User.id', // --> this is the default so you may not pass this option
'fields' =>
array(
'Info.sex', // --> %1
'Info.first_name', // --> %2
'Info.last_name', // --> %3
),
'order' => "Info.first_name, Info.last_name", // -- if you want to sort the option
'condition' => "User.manager_id=2 AND Info.lastname='Smith'", // --> if you need some filtering
)
);
$this->set(compact('users'));



$users array will contain the following structure:



array(
'User.id1' => 'Info.last_name1, Info.first_name1 (Info.sex1)',
'User.id2' => 'Info.last_name2, Info.first_name2 (Info.sex2)',
'User.id3' => 'Info.last_name3, Info.first_name3 (Info.sex3)',
.
.
.
'User.idN' => 'Info.last_nameN, Info.first_nameN (Info.sexN)',
)


Hope this post can help you in your future cakePHP 1.2 development.

Thanks to http://teknoid.wordpress.com/2008/09/04/findlist-with-three-or-combined-fields/ for giving me inspiration to develop this function.

Saturday, February 14, 2009

CakePHP Routing With Parameters

Anything you need to know with cakePHP routing can be found here.

Here is an example to illustrate routing with parameters.

/config/routes.php

Router::connect(
'/:controller/:year/:month/:day',
array('action' => 'index', 'day' => null),
array(
'year' => '[12][0-9]{3}',
'month' => '(0[1-9]|1[012])',
'day' => '(0[1-9]|[12][0-9]|3[01])'
)
);


Parameters can be access like this:

/controllers/controller.php

function index(){
// params is stored in $this->params
$year = $this->params['year'];
$month = $this->params['month'];
$day = $this->params['day'];

//
}

Friday, February 13, 2009

Increase Performance of Loop with Large Arrays

In PHP, this is how we usually loop through arrays:


<?php
for ($i=0; $i<count($big_array); $i++){
//
}
?>


Having this approach, program will try to count the $big_array every time it loops and it may cause some performance issues. To make it more efficient, we should code it this way:


<?php
for ($i=0, $n=count($big_array); $i<$n; $i++){
//
}
?>


It does the counting during initialization only.