Pages

Friday, June 3, 2011

JSON array to populate 1 lakh and more records

I recently had a problem.....which i want to discuss with you......
I am using single table concept in my database. since i am using single table i need to retrieve 11 values from the DB to form a single record. And i had 1 lakh records that means i need to retrieve 11 lakh values and form the records by looping which is so so expensive. And i am using jquery data table to display, which will take minimum of 10 seconds to load 1000 records so think about loading 1 lakh records will it load in 30 minutes definitely not it will make server to hang then everything slows down to dead. So we used flat file concept with json array to populate it with my additional functionalities like
1. Own Pagination.
2. Loading latest 100 records first and the remaining with our pagination.
3. Used own parser to parse the json data because json itself has some issues like maximum depth and importantly incompatibility with old php versions. (the json_decode function returns error by i cant find it where because i am using php5.2.6 which doesn't support json's last error) and also this project is developed 2 years back and has 400 MB of data so the transformation to the latest version is nearly impossible because every second data is coming from different sources from mobile app, mobile SMS, Web.
We solved this using our own concepts which i explained think of what, the performance is really great it took 2 to 4 seconds to load.
Its really a great experience for me.

Sunday, April 24, 2011

Facebook’s Open Compute Project

Facebook just released Open Compute Project, their now-public datacenter and server design, optimized for situations in which hundreds or thousands of servers are needed such as the biggest websites and web-hosting companies.

The hardware is somewhat similar to what Google revealed in 2009, but with a different approach to battery backups, and the complete release of the formal design documents so that anyone else could, theoretically, copy Facebook’s design and potentially improve it

Check it Facebook’s Open Compute Project – Marco.org

Wednesday, April 13, 2011

Calling ajax function when using form Widgets in doctrine and symfony1.4

Suppose you want to call a ajax function for a dropwon value changes
first define a dropdown in TblNameForm.class.php

$this->widgetSchema('myDropdown',array('label'=>'Drop Down','choices'=>array('Cars'=>'Cars','Bikes'='Bikes'),'onchange'=>'callajax(this.value,'automobileDiv')'));

In your template action class
$this->form = new TblNameForm();

In your template
<tr>
 <td>
  <?php echo $form['myDropdown']->renderLabel(); ?>
 </td>
 <td><?php echo $form['myDropdown']->render(); ?>
 <div id='automobileDiv'></div></td>
</tr>

<script type="text/javascript">
function createInstance()
{
  if (window.XMLHttpRequest)
  {
   req = new XMLHttpRequest();
  } 
  else 
   alert("XHR not created");
 return(req);
};

function callajax(val,div)
{
  var req = createInstance();
  req.onreadystatechange = function()
  { 
   document.getElementById(div).value="Wait server...";
   if(req.readyState == 4)
   {
    if(req.status == 200)
    {
     document.getElementById(div).innerHTML=req.responseText; 
    } 
   else 
   {
    document.ajax.dyn.value="Error: returned status code " + req.status + " " + req.statusText;
   } 
} 
}; 
req.open("GET", "formWidgets/automobiles?type="+val, true); 
req.send(null);
}
</script>
In automobiles function in action class
$this->type = $this->requestParameter('type');
In automobilesSuccess.php
<?php if($type=="Cars") { ?>
<select>
 <option>BMW</option>
 <option>RR</option>
 <option>Benz</option>
</select>
<?php } else { ?>
<select>
 <option>Pulsar</option>
 <option>RR</option>
 <option>BR</option>
</select>
<?php } ?>
Please double check the path to the ajax file in fresh page and after submitting the page with required error.
Because the url might change so that you need to change the path to the ajax file to work perfectly.

Hope this helps someone........

Required Error does not appears next to field in symfony Doctrine formWidgets

If you are using form widgets and drawn the fields in the template using reder() function you might get this kind of error.ie required errors will not display.

If a form is submitted it will be validated but will not display error/required information in production and shows errors in debug toolbar in dev version then this would the solution use renderError to show the errors as this
<?php echo $form['name']->renderError(); ?>
<?php echo $form['name']->renderLabel(); ?>
<?php echo $form['name']->render(); ?>

Hope this helps someone.........

_csrf_token required error in Symfony

When we are using formWidgets csrf_token required error occurs
There are certian reasons for that and have solutions for that
First we see when this error will occur
1. When you are using render function in your template
2. When you are adding an extra field
3. When you are changing the behaviour of the fields
4. When we are not binding the form

Solutions
1. When you are using render function in your template
use the below code in your template
<?php echo $form['_csrf_token']->renderLabel() ?>
<?php echo $form['_csrf_token']->render() ?>


afer that refresh the page and view the source code of the page it should have a hidden field called _csrf_token and the value should not be empty. Suppose if it is empty check the csrf token value in your setting.yml it should have some value.

After adding refresh and submit your page. Now it should not ask for _csrf_token required error. If it does then see for next possible solution

2. When you are adding an extra field
You might missed the validation for the field. Please add the validation
and check out this for adding a extra field.If the validations are correct and still the error occurs then see the next possible solution

3. When you are changing the behaviour of the fields
This is a rare condition which would occur. The situation is this ou have changed the bahaviour of the control/field in your template/form/base form class but validations may not match for the control you have modified. If so then _csrf_token error would occur. So please check it once again and check the next solution

4. When we are not binding the form
This would be the error we make most of the times. When we submit the form we should not directly save the form it should be binded and then it should be checked isvalid and then we have to save the form. Sample function to save a form could be like this


public function processForm($request sfWebRequest, $form sfForm,$redirect)
{
$form->bind(
$request->getRequestParameter($form->getName()),
$request->getRequestParameter($form->getFiles())
);
if($form->isValid())
{
$form->save();
$this->redirect($redirect);
}
}

Hope this helps someone.....

Adding a extra field in formWidgets

Class files invloved
1. TblNameForm.class.php

//Adding an extra field ---here we are adding a text field

Inside the configure function inside TblNameForm.class.php add a field
$this->widgetSchema('extraField',new sfFormInputText(array('label'=>'My Extra Field')));

Now add a validation for the extra field
$this->setValidator('extraField',new sfValidatorString(array('max_length'=>255')
));

Int the BaseTblNameForm.class.php add

$this->validatorSchema->setOption('allow_extra_fields', true);

Thats it we have added a extra field, Last and final thing to do is drawing the field in the template

your templateSuccess.php

<?php
echo $form['extraField']->renderLabel();
echo $form['extraField']->render();
?>

Thats it we have sucessfully added an extra field.

Handling form widgets in Symfony1.4 With doctrine

When you are using formWidgets you might need to change in
1. BaseTblNameForm.class.php
2. TblNameForm.class.php

Let we see something detailed about these classes
1. BaseTblNameForm.class.php

This class contains a setup function which has
1. All the fileds that are in the table.
2. All the validations of the fields.
3. These class decides the basic bahaviour of the controls ie. input, select, etc,.
4. Basic Validation type can be changed here.

Uses of defining bahaviour of the controls in BaseTblName.class.php
1. When a form is drawn without any configuration then the default bahaviour is displayed.
eg. If you are using a filed gender and control you are using is a dropdown/Raido button, then we can change the behaviour in base class itself since it is not going to change for different templates.

2. TblNameForm.class.php
This is a class which extends BaseTblNameForm.
This class contains a configure function which contains
1. All the configurations of the fields
2. Controls bahaviour can be overwritten.
3. Controls or fields cna be unset here.
4. we can also specify the validations here also
5. Extra fields should be specified here with validations

Uses of defining behaviour of the controls in TblNameForm.class.php
1. We can change the dafult behaviour of the control.
2. Since the behaviour of the controls is overwritten you have to specify the validation schema too accordingly.
3. Adding an extra can be specified here with validation.

Hope this helps someone.........

Bind an interface to more than one class using Guice

Normally you can bind an interface to only one class. But this would not be enough for an application, because we want to use a same interface to bind with more than once class we can achieve this using BindingAnnotation in Guice. A more simplistic example here.

Thursday, March 31, 2011

Guice Intro

Hi all.....
I recently came to learn about a new technology and frameworks which i  gonna share about here........
First tech is Guice.....
Guice is a DI(Dependency Injection) Framework. There are many DI frameworks, Guice is one of them which is used by google.
Advantage of using DI, increases the performance of the system and it's easy to learn.

Before explaining about Guice , first i'll explain why we go for DI.
In OOPs language we use classes, interface and concepts like inheritance to increase the performance but is it really increasing the performance of the system, the answer is partly no. but how?
lets see an example explaining this slow performance
Suppose we have four classes and name it A,B,C and D.
In which we inherited A in B, B in C, and C in D.
1.Here the class D may not use methods of A and B instead memory is allocated for all the methods inside D
2.Suppose class D wants to use class A's method, it has to move from bottom to top hirerachy.

because of these reasons , obviously reducing the performance we go for DI.

So now we see what is DI and using a simple DI usage.
Here we are creating a dependency for a class only if it is neccesary using interfaces. so unneccessary hirerachy is removed.
Which obviously increases the performance.

Now we see a simple example
1. Create an interface.
package add.service;

public interface Add {
 
    public int add(int a, int b);
 
}
2.Create a class which implements the interface
package add.service;
public class SimpleAdd implements Add{



    public int add(int a, int b) {
        return a + b;
    }

}
3. Now we are going to inject the dependency to the class by creating a Module
package add.service;
import com.google.inject.Binder;
import com.google.inject.Module;
public class AddModule implements Module{
    public void configure(Binder binder) {
        binder.bind(Add.class).to(SimpleAdd.class);
    }
}
4. Now we are creating the client and using the DI
package add.service;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class AddClient {

    public static void main(String[] args) {

        Injector injector = Guice.createInjector(new AddModule());
        Add add = injector.getInstance(Add.class);
        System.out.println(add.add(10, 54));
    }
}
So the Add functionality is directly linked using the binder and doing so increaes the performance. But we can't bind a single interface to multiple classes as same a s above.So how to bind mulitple classes? will be posted in future.

Sunday, March 13, 2011

Multiple graph in a single page using JPGraph

Implementing multiple graph in a single page is easier than you think.
You can achieve it in two ways,
First way would be combining two graphs and displaying in a page, this is a normal/usual way.
Here you can you go to see how to use it.

Second way is using the image tag here i will explain how to use it

first you decide which graph you want to use. here i take the bar graph but it doesn't matter what graph you are going to draw/implement.

here you can find the sample bargraph and save it as bargraph.php

Now we are going to draw multiple graph, here we are going to draw two graphs.

first create a image tag were the source path refers to the bargraph.php like this

<html>
<head></head>
<body>
<img src="bargraph.php">
<img src="bargraph.php">
</body>
</html>

and save it as multipleGraph.php
thats it we have created a page containing two graphs in it.

Creating dynamic graph is also easier
simply pass the values for the variable "$data" in bargraph.php as below
( here we are using "-" to differentiate between values)

<img src="bargraph.php?data=1-2-3">

and change the variable "$data" in bargraph.php page as this
$part = explode("-",$_GET['data']);
$data = array();
for($i=0;$i<count($part);$i++)
{
$data[] = $part[$i];
}

thats it you have created dynamic graph.
Hope this is useful.

Thursday, March 3, 2011

Using javascript inside an ajax page

Hi all........

We normally use javascript to call ajax page to generate/formulate content and display the content using reponseText. But today i have tried to create a javascript function inside the ajax page to perform validation.
But this doesnot work fine even the responseText loaded with all the functions. but why, because all the javascript functions and inclusion of files are done when the page loads first time. since the javascript functions defined in ajax page it is not successfully loaded. So how to achieve this i'll tell you the simpler way. JUST load all the functions in the first page itself, and all the operations performed by javascript when loading the ajax page should be define inside the functions, you need to create and use inside the functions. i.e you should not set a value for control when loading a ajax page. The complete illustration is below

for eg:
function ajaxCall()
{
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET','ajaxPage.php',false);
xmlhttp.send();
document.getElementById('div').innerHTML = xmlhttp.responseText;
}
ajaxPage.php
//setting a value like this doesn't work
<input id='date1' type='text'>
<button id="trigger1">...button>
Calendar.setup(
  {
   inputField  : "date1",
   ifFormat    : "%m/%d/%Y %I:%M %p",
   button      : "trigger1",        
showsTime   : "true",
   timeFormat  : "12"    
} );

//the button trigger1 doesn't trigger anything in the main page since the javascript function is not inside the main page, so the above functionality as below to make it work.

first create a function
function setDate(val)
{

Calendar.setup(
  {
   inputField  : "date"+val,
   ifFormat    : "%m/%d/%Y %I:%M %p",
   button      : "trigger"+val,        
showsTime   : "true",
   timeFormat  : "12"    
} );


}

and include this function in the main page
after including change the code in the ajax page as below


<input id="date1" type="text" />
<button id="trigger1" onclick='setDate(1)'>...button>

now this will work fine.

i hope this helps someone.

Tuesday, March 1, 2011

Including a php in symfony

Hi....
Including a php inside symfony framework is a very easy thing. Let me explain it can be done.

You can do this in two ways
i) First one is the normal and logical way is to use the templates and displaying using sfConfig
refer this Including PHP

ii) second one is simpler to use for which u need not use action or templates.
To achieve this u just need to call that particular php file inside an iframe, that would be the simpler way to include a php file.

Sometimes u may need to open this page in a thickbox, at this time u will be calling the page in body onload function but this may not workout sometimes, but u can the delay option and then use the iframe to display the page.This will work fine.

Friday, February 25, 2011

Session handling solved.....


Hi....
 Handling a sesion is always an important issue.

Normally errors will be thrown in default, when there is an issue or errors in sessions handling but sometimes this may not work.

One of that is what we came across recently
When we developed a website in our home country and the servers also resides in same place there is no errors or warnings related with sessions.
But after we shift the server to other country it started to throw warning like
"Session alreday created" even after we cleared old sessions using ob_start();
and started a new session
session_start();

the warnings occured because these functions were declared after some PHP coding
but the same code never thrown any warnings ever in local(the server when it is in home country).

Tips:
1. You can avoid showing unwanted warning/errors by using error_reporting(0)

So next time you got error or warning check you have called these functions at the beginning of the file.

Date and timezone issue solved



Hi....
I just faced an important problem when developing a website in PHP where the server

resides in one country and used in another.

The situation is this,
We have developed the website for indian users which resided in indian server
Recently we have changed this server to US.
The website is based on lots of date related functionalities  such as Calendar, Record

Created Date, Updated Date, History,etc,.
Since the server resides in US and the website really used and developed in India,

because of that, the results were shown with false data to the users of india wherever

date is displayed.

After sometime i found a solution

The solution to this would be using the timezone i.e

date_default_timezone_set("Asia/Calcutta") but even this will not solve all the issues

especially when you are inserting a date into the DB directly for eg using now()

"insert into tbloutgoing set sent_date=now()";

executing the above query will insert date and time of the US even you set the dafault

timezone but why, because the defualt_timezone is a function inside PHP not the server

function, the above query will execute inside phpmyadmin which is in US so the error,
This has two solutions,

The Easy way
1.You need to use timezone functionality
 Storing the date to a variable and inserting
    eg:
$mydate = date('Y-m-d');
"insert into tbloutgoing set sent_date='".$mydate."'";

The hard way
2.For this you need not use the timezone functionality.
 First you have to find the difference of time i.e US and India

Once you find it you can insert the date directly into the DB
   eg:
"insert into tbloutgoing set sent_date=now()+5.30";

Tips
1. include the timezone functionality.

Advantages of using easy way
1. You can shift the server to any country at anytime, and you need to change only the

timezone and in only one place.

Disadvantage of using the hard way
1.Shifting the server to another country.
   This would be a great disadvantage of using the hard way which may consume lot of

time, you have to find the difference and change it whereever you used date

functionality.


But i really wounder why this basic and important functionality doesnot exists  as a

default function in server.