Search This Blog

Thursday, May 19, 2011

Print all form errors in a symfony form

foreach($form->getWidgetSchema()->getPositions() as $widgetName)
{
  echo $form[$widgetName]->renderError();
}


or you can use

$this->form->getErrorSchema();

Monday, May 2, 2011

Installing Apache, PHP, MySQL on Ubuntu


Install Apache 

Open Terminal (Application -> Accessories -> Terminal) and execute the following command:
sudo apt-get install apache2

check if the Apache is working properly by pointing your browser to http://localhost. If you see the text “It works!”, it means Apache is working.

Issue the following command to resolve following error Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

sudo  gedit /etc/apache2/conf.d/fqdn

When Gedit opens, type “ServerName localhost” inside the file and click Save

Install PHP



Inside Terminal, execute the following command:
sudo apt-get install php5 libapache2-mod-php5

execute this to restart apache

Execute the following command in Terminal:
    sudo /etc/init.d/apache2 restart

Install MySql

sudo apt-get install mysql-server



Inside Terminal, execute the following command:
sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

Give the root password to complete the installation


Setting up phpunit in ubuntu



  1. Install PEAR . Issue the command sudo apt-get install php-pear.
  2. Make sure PEAR is up to date. Issue the command sudo pear channel-update pear.php.net
  3. Upgrade the PEAR elements. Issue the command sudo pear upgrade-all.
  4. Tell PEAR where to find the PHPUnit code. Issue the command sudo pear channel-discover pear.phpunit.de
  5. Install PHPUnit. Issue the command sudo pear install -a phpunit/PHPUnit. The -a makes sure all dependency packages are also installed.
  6. Issue the command phpunit --version. You should have version 3.3.17 or newer installed.
  7. Install XDebug. Issue the command sudo apt-get install php5-xdebug. This is needed if you want the HTML based coverage reports. 
  8. Restart the web server after installing this package. (sudo /etc/init.d/apache restart ).
reference 

Saturday, April 30, 2011

Best way to create a CSV file in PHP

I have seen many php scripts trying to create CSV's using strings and adding commas inside a for loop. But this method is prone to errors since it doesn't work properly with special characters like comma and quotation. This function use php fputcsv to create a CSV file in the server memory rather than the disk making its faster to retrieve the CSV file.

$headerArray = array('Mon','Tue','Wed') ;
$bodyArray = array(array(1,2,3),
array(4,5,6));


function makeCsv($headerArray,$bodyArray) {    

        $headerIncluded = false;
        $fp = fopen('php://temp/maxmemory:' . (5 * 1024 * 1024), 'r+'); // 5MB of memory allocated 

        $content = $bodyArray;

        foreach ($content as $row) {

            if(!$headerIncluded) {
                $headerIncluded = true;
                fputcsv($fp, $headerArray); // create the csv file in the memory
            }
                fputcsv($fp, $row);
        }

        rewind($fp);

        $output = stream_get_contents($fp); // get csv file from the memory

        fclose($fp);        

        return $output;
    }


Get terminal in right click context menu in ubuntu

If you want to get the open a folder in terminal by just right clicking on a folder run this command as the super user.
sudo apt-get install nautilus-open-terminal

Wednesday, March 16, 2011

Find Year difference in SQL


YEAR(CURRENT_DATE)-YEAR('1971-01-01') as years_now


Find Future Date from PHP

date("Y-m-d", strtotime(date('Y-m-d') . " +6 month"));

Tuesday, February 22, 2011

How to Exclude a tag from css class

form#frmBilling  label:not(.formRadio){
 width: 140px;
}


The above width will be applied to all the labels in the form except
labels with the class formRadio.

Tuesday, February 15, 2011

How to get svn diff file and apply patch file

Get as a patch file

>svn diff -r 6738:6739>mychange.diff

apply the patch file

>patch -p0 -i mychange.diff