Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Friday, 19 August 2022

Design Pattern in PHP

<?php

class Booklibrary {

 private $bookName = '';

 private $bookAuthor = '';

 const BR = '<br/>';

 public function __construct($name = '', $author = '') {

  $this->bookName = $name;

  $this->bookAuthor = $author;

 }

 public function getNameAndAuthor() {

  return $this->bookName . ' - ' . $this->bookAuthor . self::BR;

 }

}

class BooklibraryFactory {

 public static function create($name, $author) {

  return new Booklibrary($name, $author);

 }

}

$bookOne = BooklibraryFactory::create('Shiv Khera', 'You can win');

$bookTwo = BooklibraryFactory::create('Norman Vincent Peale', 'The Power Of Positive Thinking.');


echo $bookOne->getNameAndAuthor();

echo $bookTwo->getNameAndAuthor();

Sunday, 19 July 2015

PHP MySQL: Getting year, month, day, hour, minute and second from DATETIME field

We all need to extract day, month and year from date provided.

For this, we often explode() the provided input and then get the required elements from the array.

There is a strong option to this.

Use substr() to get sub-string.

In Database, DATE and DATETIME fields have specified lengths.

We can use substr() in that case.

Examples:

For DATETIME:
    $dt = '2015-07-20 11:23:56';
    echo "<br/> Date: " . $dt;
    echo "<br/> Year: " . substr($dt, 0, 4);
    echo "<br/> Month: " . substr($dt, 5, 2);
    echo "<br/> Day: " . substr($dt, 8, 2);
    echo "<br/> Hour: " . substr($dt, 11, 2);
    echo "<br/> Minute: " . substr($dt, 14, 2);
    echo "<br/> Second: " . substr($dt, 17, 2);

For DATE:
    $dt = '2015-07-20';
    echo "<br/> Date: " . $dt;
    echo "<br/> Year: " . substr($dt, 0, 4);
    echo "<br/> Month: " . substr($dt, 5, 2);
    echo "<br/> Day: " . substr($dt, 8, 2);

Thursday, 18 June 2015

PHP: Generate log messages and save to a custom file.

We all know that PHP save errors in php_errors.log file.

But, that file contains a lot of data.

If we want to log our application data, we need to save it to a custom location.

We can use two parameters in the error_log function to achieve this.

http://php.net/manual/en/function.error-log.php

We can do it using:

error_log(print_r($v, TRUE), 3, '/var/tmp/errors.log');

Where,

print_r($v, TRUE) : logs $v (array/string/object) to log file.
3: Put log message to custom log file specified in the third parameter.
'/var/tmp/errors.log': Custom log file (This path is for Linux, we can specify other depending upon OS).

OR, you can use file_put_contents()

file_put_contents('/var/tmp/e.log', print_r($v, true), FILE_APPEND);

Where:

'/var/tmp/errors.log': Custom log file (This path is for Linux, we can specify other depending upon OS).
print_r($v, TRUE) : logs $v (array/string/object) to log file.
FILE_APPEND: Constant parameter specifying whether to append to the file if it exists, if file does not exist, new file will be created.

Tuesday, 9 June 2015

Yii: jQuery UI widget dialog with ok and close buttons.

Yii: jQuery UI widget dialog with ok and close buttons.

<?php
$this->beginWidget('system.zii.widgets.jui.CJuiDialog',
array(
'id'=>'dialog'.$this->id,
// additional javascript options for the dialog plugin
'options'=>array(
'title'=>$this->id.' Dialog box 1',
'width'=> '800px',
'height' => '600',
'position' => 'top',
'top' => '50px',
'modal' => true,
'buttons' => array(
"Ok"=>'js:function(){alert("xxx")}',
'Close'=>'js:function() {$(this).dialog("close");}'
),
'autoOpen'=>false,
)
)
);
?>

Reference:

Monday, 8 June 2015

Yii 1.1: Add jQuery UI datepicker to multiple input date fields.

In the following way, you can add jQuery UI datepicker to your Yii site.

$this->widget('zii.widgets.jui.CJuiDatePicker',array(
    'name'=>'publishDate',
    // additional javascript options for the date picker plugin
    'options'=>array(
        'showAnim'=>'fold',
    ),
    'htmlOptions'=>array(
        'style'=>'height:20px;'
    ),
));
You can even add date picker to more than one elements.
For example, you have two date fields with ids:
joining_date and increment_date
Then you can add these two dates in array like:
$dateFields = array('joining_date', 'increment_date');
if (! empty($dateFields)) {
// See here, you can add run time fields.
$dateFieldIds = implode(',#', $dateFields);
$this->widget('zii.widgets.jui.CJuiDatePicker',array(
  'name' => $dateFieldIds, 
  'options'=>array('showAnim'=>'fold',),
  'htmlOptions'=>array('style'=>'height:20px; display:none;'),
 ));
}

Reference:

http://www.yiiframework.com/doc/api/1.1/CJuiDatePicker

Tuesday, 26 May 2015

Include CSS and JS files in Yii 1.1

$baseUrl = Yii::app()->baseUrl; 
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($baseUrl.'/js/yourscript.js');
$cs->registerCssFile($baseUrl.'/css/yourcss.css');
Reference:

Parenting tips to inculcate learning habits in your kid

Parenting tips to inculcate learning habits in your kid Tip #1) Children do not learn things, they emitate. So, try to do things by yours...