Friday 8 August 2014

Print and Preview specific Div with Javascript

Spouse we have a DIV that we want to print and its ID is 'printPDF'

 <div id="printPDF"> This area will be print</div>
var style_sheet = 'baseUrl . "/css/style.css"; ?>'; //Css file,Base url
var style_sheet = '';// Style sheet that you want to include


 var printContent = document.getElementById('printPDF').innerHTML;
        var printContent2 = "" + style_sheet + "" + printContent + "";
        var windowUrl = 'about:blank';
        var uniqueName = new Date();
        var windowName = 'Print' + uniqueName.getTime();
        var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=0,height=0');
        printWindow.document.write(printContent2);
        printWindow.document.close();
        printWindow.focus();
        printWindow.print();
        printWindow.close();

Wednesday 6 August 2014

Playing With Date in PHP

PHP program that prints every date from your birthday to three months after. On the 1st of each month, write the month name. Write “Free lunch” next to fridays and on dates where the day number is multiple of five print “Casual” next to the day {Day Name}.
Sample Section of Output
18 Mar 1986
19 Mar 1986
20 Mar 1986 - Casual Thursday
21 Mar 1986 - Free lunch
22 Mar 1986
23 Mar 1986
24 Mar 1986
25 Mar 1986 - Casual Tuesday
26 Mar 1986
27 Mar 1986
28 Mar 1986 - Free lunch
29 Mar 1986
30 Mar 1986 - Casual Sunday
31 Mar 1986
1 Apr 1986
format("d M Y");

    $get_day = date('d M Y', strtotime($get_day));
    $day_of_month = date('d', strtotime($get_day));
    $toChecksat = date('D', strtotime($get_day));

    if ($day_of_month == '01') {
        $first_date_of_month = new DateTime($get_day);
        echo $first_date_of_month->format('F') . "
"; echo $all = $date->format("d M Y") . "
"; } else { if ($toChecksat == 'Sat') { //Check for SATURDAY echo $all = $date->format("d M Y") . " - Free lunc" . "
"; } else if (($day_of_month % 5) == 0) { //devided by 5 $full_day_name = new DateTime($get_day); echo $all = $date->format("d M Y") . " - Casual " . $full_day_name->format('l') . "
"; } else { echo $all = $date->format("d M Y") . "
"; } } } ?>

Thursday 16 January 2014

Error logs save in Db in Yii


1. Yii have its own method to save error logs in DB, follow the steps below in config/main.php Make new db connection (Separate db to save logs)
'log_db'=>array(
          
                        'class'=>'CDbConnection',
                        'connectionString' => 'mysql:host=localhost;dbname=db_name',
                        'emulatePrepare' => true,
                        'username' => 'root',
                        'password' => '',
                        'charset' => 'utf8',
                       
        ),
2. We can use yii default class to save logs in db "CDbLogRoute"
'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                /**/array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning',
                                       
                ),
                // uncomment the following to show log messages on web pages
               
                array(
                    'class'=>'CWebLogRoute',
                ),
                           
                                array(
                    'class'=>'CDbLogRoute',
                             'connectionID'=>'log_db',   
                             'logTableName'=>'yii_logs', //log table name
                             //'categories'=>'error, warning',
                              'levels'=>'error, warning',
                             //'autoCreateLogTable'=>true,
                ),
               
            ),
        ),

3. Yii default table structure to save logs. (It will created automatically if 'autoCreateLogTable'=>true) )
CREATE TABLE IF NOT EXISTS `yii_logs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `level` varchar(128) DEFAULT NULL,
  `category` varchar(128) DEFAULT NULL,
  `logtime` int(11) DEFAULT NULL,
  `message` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
Table snapshot with real data
4. You can save custom fields in Yiilog For this you can make your own class and extend to CDbLogRoute (save in protected\components folder) Like this
createCommand()->createTable($tableName, array(
            'id'=>'pk',
            'level'=>'varchar(128)',
            'category'=>'varchar(128)',
            'logtime'=>'integer',
            'message'=>'text',
            'users_id'=>'int(11)',
            'ip'=>'varchar(50)',
            'error_code_id'=>'INT(11)',
            'output_variable'=>'varchar(225)',
            'exception_id'=>'varchar(45)',
                        'exception_category'=>'ENUM("CApplication", "CDbException", "CHttpException", "BException")',
                        'updated_date'=>'timestamp',    
                        'created_date'=>'timestamp',
            
        ));
    }
    

    
    protected function processLogs($logs)
    {

        $command=$this->getDbConnection()->createCommand();
               
                  foreach($logs as $log)
        {
                  $clog  =     explode("::", $log[0]);
                  $elog=CJSON::decode($clog[0]);
                                     
               $command->insert($this->logTableName,array(
                'level'=>$log[1],
                'category'=>$log[2],
                'logtime'=>(int)$log[3],
                'message'=>$log[0],
                                'users_id'=>Yii::app()->user->getId(),
                                'ip'=>Core::getIP(),
                                'error_code_id'=>$elog['error_code_id'],
                                'exception_id'=>$elog['exception_id'],
                                'exception_category'=>$elog['exception_category'],
                                'output_variable'=>$elog['output_variable'],
                                                       
            ));
                      
        }
    }
       



and also some change in main .php
'class'=>'application.components.ELog',
                                        'connectionID'=>'log_db',
                                        //'logTableName'=>'yii_logs',
                                        //'categories'=>'error, warning',
                                       //'levels'=>'error, warning',
                                       'levels'=>'elog',   
                                       'autoCreateLogTable'=>true,
    )

 
In controller you can call it like this
Yii::log(CJSON::encode(array('exception_id'=>00001','error_code_id'=>'1','output_variable'=>'user_id','exception_category'=>'BException'))."::", 'elog');

 

Wednesday 1 January 2014

PHP function Returne correct IP address even if user is behind proxy

 
/**
     * Returne string correct IP address even if user is behind proxy
     * @return string correct IP address even if user is behind proxy
     * @return type
     */
    public static function getIP() {
        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        }
        elseif (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_X_FORWARDED')) {
            $ip = getenv('HTTP_X_FORWARDED');
        }
        elseif (getenv('HTTP_FORWARDED_FOR')) {
            $ip = getenv('HTTP_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_FORWARDED')) {
            $ip = getenv('HTTP_FORWARDED');
        }
        else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

Friday 6 December 2013

Yii vs Magento


Yii Magento
Category Framework Online Shop System
Web application framework

Preference (53% global votes) (43% global votes)
Website www.​yiiframework.​com www.​magentocommerce.​com
License New BSD License OSL 3.0
System requirements
Operating system Cross-platform Cross-platform
Programming language PHP PHP
Memory recommended ? 512 MB
Memory minimum ? 256 MB
More
Description Yii is a high-performance, component-based PHP framework for developing large-scale Web applications rapidly. It enables maximum reusability in Web programming and can significantly accelerate your Web application development process. Magento is a Dynamic & Robust eCommerce Platform
Tag MVC ?
Multi-user system Yes Yes
Autofocus Yes ?
Pingback Yes Yes
Extension/Plug-in Yes Conditional
Interpreter Yes ?
Database MySQL MySQL
SQLite
MSSQL
Oracle
PostgreSQL

Trackback Yes Yes
Multilingual content Yes Yes
Database model Relational Object-relational
Object-oriented

Transactions Yes ?
Unicode Yes Yes
WYSIWYG-Editor Yes Yes
Multiple projects Yes Yes
Standard compliance Yes Yes
External pages Yes No
User statistics Yes Yes
Revision control Yes Yes
Atomicity Yes ?
Isolation Yes ?
Horizontal scalable Yes No
Template language Mustache PHP
drupal
Markdown
Smarty
Twig
PHP

Target audience Beginner Web Development
SMB
Web Development
Cloud computing
Government
App developer
System Adminstration

Framework Yii Zend Framework
Full text search Yes Yes
Scaffolding Yes Yes
Design pattern Active-Record Model-View-Controller
Model-View-Controller
DAO
HMVC
Observer

Development principles Convention over configuration ?
Test-driven development
Don't repeat yourself

Difficulty level Intermediate Intermediate
Application startup time 1 ms 1 m
Version number 1.1.14 1.8
Adobe Flash Support Yes Yes
PSR-0 compliant Yes ?
Object-Relational Mapping (ORM) Yes Yes
Popularity (60% global votes) (67% global votes)
Machine Code Generation Yes Yes
Documentation level ★★★★☆ ★★★☆☆
Multi tasking Yes ?
RESTful Yes Yes
Wastebag Yes ?
Code Generation Yes ?
Dynamic typing Yes Yes
Composite keys Yes ?
Programming paradigm Object-oriented Object-oriented
Event-driven

Comments Yes Yes
Multiple categories Yes Yes
Hierarchical menus Yes Yes
Scripting language support JavaScript Prototype
PHP JavaScript
Free to use Yes Yes
Backend PHP PHP
Active Yes Yes
Database Connection Pooling No Yes
Separate Service Layer Yes ?
Web Flows Yes ?
Heroku Support No ?
Creation Date Jan-08 2008
Community Driven Excellent Basic
Wizard Yes ?
WSDL Yes ?
Reliability ★★★★★ ★★★☆☆
Versioning Good Basic
Custom queries Excellent Basic
Input Widgets Yes ?
Layout Structure Template Yes Yes
File Assets Yes Yes
API Good Good
Compiled language No ?
XML Aware Good Good
Client/Server code reuse Model-View-Controller ?
Batch Processing Yes ?
Cloud platform support Amazon EC2 ?
Amazon S3
digital ocean
ucloud biz
OpenShift
Windows Azure
Linode

Admin Generator Yes Yes
Compiler No ?
Library file size 10 MB ?
API comprehensibility ★★★★☆ ★★☆☆☆
Webmail Yes Yes
Jobs Oportunities ★★★★☆ ★★★★★
Implementation flexibility (49% global votes) (0% global votes)
Out-of-the-box functionality (53% global votes) (25% global votes)
Throttling Yes ?
Bundle system Yes Yes
Annotation Support Yes ?
LDAP Yes No
Query Cache Yes Yes
Realtime Yes ?
Data Security Yes Yes
Community feedback ★★★★★ ★★☆☆☆
Ease of use ★★★★★ ★★★★☆
Malicious Injection Prevention Yes ?
Openshift Paas Support No ?
Copy, Cut and Paste Support Yes ?
Free for commercial use Yes ?
ORM Yes ?
Realtime Server Push Yes Yes
Easy of Use Yes ?
Sexiness 100 101
Open Source Yes Yes
Web Developer Toolbar Yes ?
XQuery Support No ?
Websocket Support No ?
Maven support Yes ?
Resource File Processing Yes ?
Partial Classes Yes ?
Perfomance ★★★★★ ★★★★☆
Database migrations Yes Yes
I like it Yes Yes
Debug Mode Yes Yes
Static Typing Yes ?
Click & Edit in Place / WYSIWYG Yes ?
Autocomplete Code Yes ?
UI framewrok 8 ?
Makes you angry No Yes
Test 3 g ?
Easy to Learn Yes No
Ajax Yes ?
User management Yes ?
ORM Join Support Yes ?
Object-Oriented Views Yes ?
Object Oriented Models Yes ?
Mobile ready Yes ?
ACL Yes ?
Requests per second 851 ?
Operating system server Cross-platform ?
Package Manager Yes ?
Easy Setup Yes ?
Jquery Yes ?
Costanzia Yes ?
Grey 50 m ?
Protein ? 18
Browser support ? Firefox
Internet Explorer
Chrome
Safari