Social Icons

Thursday, May 30, 2013

Installing and Setup Yii framework - PHP

Yii framework is one of the popular PHP MVC framework these days. Here, I am providing a guide to install yii framework. Just make sure your web server is capable of supporting PHP 5.1 orr higher. I assume that you have already installed web server and PHP or softwares like wamp, xampp or lamp. So, here we go.

First, download the yii framework from http://www.yiiframework.com/download/ . Unpack the downloaded file to a web accessible folder. For example, if you have xampp installed then place it inside xampp/htdocs/ , if wamp then you can place it in wamp/www/ . Now, you can check if your server satisfies all requirements for using yii and the installation was a success. I assume you have installed xampp server and put the downloaded yii folder inside xampp/htdocs/ . Now, open the url in your browser as http://localhost/yii/requirements/index.php You will see the results there. Few fields may show warnings like for PDOSQLite extension or memcache extension. But that is ok. You can proceed. Now, lets talk about creating a new application in yii.

Creating a new application in yii

We will use a command line tool 'yiic' that comes packaged with he yii framework. It is not mandatory to use this toll but it saves time and helps in creating a new yii application easily. Now, open your command terminal and change to your webroot folder i.e, till xampp/htdocs.

cd xampp/htdocs

If you have installed wamp then you have to reach to wamp/www. Here, I assumed you have xampp installed.

Now, as you put yii downloaded folder inside htdocs as xampp/htdocs/yii, run the command like this...

yii/framework/yiic webapp demo

Here, 'demo' is your project folder name.

Note: If you get the error here as : php.exe is not recognized as an internal or external command then look here for the solution: http://awesomesirjee.blogspot.in/2013/05/phpexe-is-not-recognized-as-internal-or.html

On running the above command, it will give you message as :

Create a Web application under '/xampp/htdocs/demo'? [Yes|No]
Yes

It will create you application successfully. You can check a new folder named 'demo' is there in xampp/htdocs/ . You see how easily you created a new brand yii application with just one command. Now, got your browser and type in url: http://localhost/demo/index.php

You will find your new application running with a welcome page. Thats it.

php.exe is not recognized as an internal or external command - yiic - yii framework

This is a common error that you can come across while creating a new yii application using 'yiic' command tool. If you get this error, follow the below steps and your error will be fixed.

You just need to add complete path of your php.exe in yii/framework/yiic.bat
yiic.bat is the MS-DOS batch file.
For example in my case, it will be

if "%PHP_COMMAND%" == "" set PHP_COMMAND=C:\xampp\php\php.exe

This solution will work only for yii.

If you want to make it for all i.e make it possible to run php.exe in any command line instance, then you will have to add path of php.exe in your PATH environment variables.

Monday, May 27, 2013

PHP extension "curl" must be loaded - Magento

While installing magento, you can get this error on configuration part.

But you can resolve it easily. Magento installation wizard now checks for curl on setup. Just make sure that curl extension is enabled in your php.ini

You can uncomment the line referring to php_curl.dll and restart your web server. It should fix the error

Friday, May 24, 2013

Include all PHP files from a directory


Suppose you have a directory (or say folder) consisting of some PHP files and you want to include all of them in your code. You need not to write include for each and every file. Then, you can do it in one line as follows:

<?php
foreach (glob("classes/*.php") as $filename)
{
    include $filename;
}
?>

Thursday, May 23, 2013

Export HTML table to excel xlsx in php

Many a times, it has been a requirement in PHP, you have to generate a report and down the html table report to excel. Here, I am providing the solution as how can you do it in PHP.

You can achieve this using PHPExcel library. First download PHPExcel library and put in your project folder. Then, apply the below code to generate excel report. Here is the code.

<?php
$arr['name'] = array('Prem', 'Shailesh', 'Rahul');
$arr['age'] = array('23', '21', '25');


$filename ="Excel_Report.xls";
$contents = "Name \t Age \n";


include('PHPExcel.php');
for($i = 0; $i < count($arr['name']; $i++)
{
   $contents.= $arr['name'][$i]. " \t";
   $contents.= $arr['age'][$i]. " \n";
}


header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $contents;
exit;

?>

yii chtml dropdown onchange

In yii framework, you can make a dropdownlist (or select) as follows:


<?php
echo CHtml::dropDownList('select_id','',
 $data,

 array(
'prompt'=>'Select',
)
)

?>

where data is the array in the form of key=>value.

But what if you have to put here a 'onchange' event or styling too with chtml dropdown list. It can be done as follows:


<?php
echo CHtml::dropDownList('select_id','',
 $data,

 array(
'prompt'=>'Select a Industry',
'style'=>'width:250px',
'onchange'=> 'myFunc()',
)
)

?>

Now, on change of the dropdown list, it will call a javascript function myFunc().

illegal mix of collations (latin1_swedish_ci implicit) and (latin1_general_ci implicit)

It is one of the common errors in MySql.

This error comes when you are fetching data from two or more than two tables via sql query and collation of those tables is not same. You have to make sure that the tables from which you are fetching data, have the same collation. After that also check the collation type of each table field that you have used in operation.
If collations are different, then you'll get the above error as illegal mix of collations.

To resolve this, you can change the collation of a table with the below query:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET latin1 
COLLATE 'latin1_swedish_ci';

This will be the fix for the error.

Total Pageviews