How to write a wordpress plugin ( Tutorial)

If you are programmer and you are using wordpress then you always try to how to write wordpress plugin. Now i am going to write the steps of wordpress plugin writting. In More »

How can run php file through command prompt

if you have installed wamp and you want to run php file through command prompt Click On Start->Command prompt (In Windows 7) IN my System is showing C:\Users\Nanhe> Then Type Command C:\Users\Nanhe> More »

 

Bubble sort program in php

Bubble-sort-example-300px

Bubble sort program in php

function bubble_sort(&$A) {
   for ($i = count($A) - 1; $i > 1; $i--) {
      for ($j = 0; $j < $i; $j++) {
         if ($A[$j] > $A[$j + 1]) {
            $t = $A[$j];
            $A[$j] = $A[$j + 1];
            $A[$j + 1] = $t;
         }
      }
   }
}

$A = array(3, 1, 4, 6, 8, 7, 2, -1, 67, 34);
bubble_sort($A);
print_r($A);

Merge sort program in php with pseudocode

Merge sort code in php without inbuilt function


/**
*
* @author Nanhe Kumar <nanhe.kumar@gmail.com>

*/
function merge(&$A, $f, $m, $l) {
   $l1 = $m - $f + 1;
   $l2 = $l - $m;
   for ($i = 1; $i <= $l1; $i++) {
      $L[$i] = $A[$f + $i - 1];
   }
   for ($j = 1; $j <= $l2; $j++) {
      $R[$j] = $A[$m + $j];
   }
   $L[$l1 + 1] = $R[$l2 + 1] = INF;
   $i = $j = 1;
   for ($k = $f; $k <= $l; $k++) {
      if ($L[$i] <= $R[$j]) {
         $A[$k] = $L[$i++];
      } else {
         $A[$k] = $R[$j++];
      }
   }
}

function merge_sort(&$A, $f, $l) {
   if ($f < $l) {
      $m = (int) (($f + $l) / 2);
      merge_sort($A, $f, $m);
      merge_sort($A, $m + 1, $l);
      merge($A, $f, $m, $l);
   }
}

$A = array(1, 5, 2, 4, 6, 1, 3, 0, 67, 87);
merge_sort($A, 0, count($A) - 1);
print_r($A);

Insertion sort program in php with Pseudocode

Insertion-sort-example-300px

Pseudocode of Insertion short.

INSERTION-SORT.(A)
1 for j D 2 to A:length
2   key = A[j]
3   // Insert A[j]  into the sorted sequence A[1 ..... j-1].
4   i=j-1
5   while i >0 and A[i] >key
6     A[i+1] = A[i]
7     i = i-1
8   A[i+1]= key

PHP program of Insertion Sort.

function insertion_sort($A) {
   for ($j = 1; $j < count($A); $j++) {
      $key = $list[$j];
      $i = $j - 1;
      while ($i >= 0 && $A[$i] > $key) {
         $A[$i + 1] = $A[$i];
         $i = $i - 1;}
         $A[$i + 1] = $key;}
      }
      $A = array(5, 2, 4, 6, 1, 3);
      insertion_sort($A);
      print_r($A);

How to check OS and version using a Linux command

uname -a

Show the kernal version

cat /etc/issue.net

Show the operating system version

MongoDb with python program


from pymongo import MongoClient

con= MongoClient()
db=con.items.scrapy_items

#Find Single Record
item = db.find_one()
for key in item:
   print("{} = {}".format(key, item[key]))

#Find Multiple Records
items = db.find()
for item in items:
   for key in item:
      print("{} = {}".format(key, item[key]))

#Find by condition
   items=db.find({'base_id':2200455})
   for item in items:
      for key in item:
         print("{} = {}".format(key, item[key]))

#Insert record
db=con.items.test_items
data = {"base_id":2200455,"first_name": "Alfred", "last_name":"Hitchcock","path":['a','b']}
db.insert(data)
data = {"base_id":3300455,"first_name": "Alfred", "last_name":"Hitchcock","path":['x','y']}
db.insert(data)

#select command
#usert means insert data if not found,multi means update multiple
db.update({
   'base_id': 3300455
   },{
   '$set': {
      'first_name': 'Nanhe'
    }
  }, upsert=False, multi=False)

Yii Framework Session handling With Database

There is no need to much more coding it is very simple Follow Steps.

Step1: Crate Table.
CREATE TABLE yii_session
(
id CHAR(32) PRIMARY KEY,
expire INTEGER,
data BLOB
);

Step : Simply add this code to the “components” section of protected/config/main.php:

‘session’ => array(
‘class’ => ‘system.web.CDbHttpSession’,
‘connectionID’ => ‘db’,
‘sessionTableName’ => ‘yii_session’,
),

Find 2 number in the array whose difference is minimum. Array is unsorted. Complexity should be less than o(nlogn).


Q1. Find 2 number in the array whose difference is minimum. Array is unsorted. Complexity should be less than o(nlogn).
eg 2 4 6 1 2 5 – Output should be 2,2

Ans :

<?php

$list = array(2, 4, 6, 1, 2, 5);
$fn = $list[0];
$sn = $list[1];
$diff = $fn – $sn;
$diff = $diff < 0 ? ($diff * -1) : $diff;
for ($i = 0; $i < count($list); $i++) {
for ($j = $i + 1; $j < count($list); $j++) {
$no = $list[$j] – $list[$i];
$no = $no < 0 ? ($no * -1) : $no;
if ($no < $diff) {
$diff = $no;
$fn = $list[$i];
$sn = $list[$j];
}
}
}
echo $fn,$sn;

EMI Calculator code in php


<?php
/*
* @author : Nanhe Kumar
*/
class EMICalculator {

protected $_emi;
protected $_interest;
protected $_principalAmount;
protected $_interestRate;
protected $_annualInterestRate;
protected $_totalMonth;

//Setter
public function setPrincipalAmount($principalAmount) {
$this->_principalAmount = $principalAmount;
}

public function setInterestRate($interestRate) {
$this->_interestRate = $interestRate;
$this->_setannualInterestRate();
}

protected function _setannualInterestRate() {
$this->_annualInterestRate = $this->_interestRate / (12 * 100);
}

public function setTotalMonth($totalMonth) {
$this->_totalMonth = $totalMonth;
}

//Getter
public function getPrincipalAmount() {
return $this->_principalAmount;
}

public function getInterestRate() {
return $this->_interestRate;
}

public function getTotalMonth() {
return $this->_totalMonth;
}

public function getEMI() {
return $this->_emi;
}
public function getInterest(){
return $this->_interest;
}


public function showValue() {
if (php_sapi_name() == 'cli') {
$nl = "\n";
} else {
$nl="</br>";
}
echo 'Principle Amount=', $this->_principalAmount,$nl;
echo 'Interest Rate=' . $this->_interestRate,$nl;
echo 'Annual Interest Rate =' . $this->_annualInterestRate,$nl;
echo 'Total Month=', $this->_totalMonth,$nl;
echo 'EMI=', $this->_emi,$nl;
echo 'Interest=', $this->_interest,$nl;
}

/*
periodicPayment = (principalAmount*annualInterestRate)* {(1+annualInterestRate)^totalMonth / [(1+annualInterestRate)^totalMonth]-1}

*/
public function calculateEMI() {

$this->_emi = ($this->_principalAmount * $this->_annualInterestRate) * (pow((1 + $this->_annualInterestRate), $this->_totalMonth) / (pow((1 + $this->_annualInterestRate), $this->_totalMonth) - 1));
$this->_interest=($this->_emi*$this->_totalMonth)-$this->_principalAmount;
}

}

$emi = new EMICalculator();
$emi->setPrincipalAmount(100);
$emi->setTotalMonth(3);
$emi->setInterestRate(12);

$emi->calculateEMI();
$emi->showValue();
?>

PHP Fatal error: Maximum execution time of 0 seconds exceeded


I was running a long PHP script. Error coming was :

Maximum execution time of 30 seconds exceeded. I tried searching on google that Way i made a change ,

ini_set(‘max_execution_time’, 0);

I again run the script but could not solve the purpose and error converted into

Maximum execution time of 0 seconds exceeded.

then i Made the following change and that solved the purpose.

ini_set(‘max_execution_time’, -1);

 

Its working huraa!!!!!!!!

Runs JavaScript code on the MongoDB database server


$m = new MongoClient();
$db = $m->test;

$inset = "db.getCollection('foo').insert({'name':'nanhe','age':30});";
$response = $db->execute($inset);
print_r($response); //Array ( [retval] => [ok] => 1 )

$response = $m->test->execute("db.getCollection('foo').insert({'name':'happy','age':18});");
print_r($response); //Array ( [retval] => [ok] => 1 )

$response = $m->test->execute("db.foo.insert({'name':'prince','age':16});");
print_r($response); //Array ( [retval] => [ok] => 1 )

$response= $m->test->execute("return db.foo.count();");
print_r($response); //Array ( [retval] => 3 [ok] => 1 )

$response= $m->test->execute("return db.foo.findOne();");
print_r($response); //Array ( [retval] => Array ( [_id] => MongoId Object ( [$id] => 5287ccbe60e2eac9a0e2f1c6 ) [name] => nanhe [age] => 30 ) [ok] => 1 )

/*
* If you want use find function then use toArray because The find() function returns a cursor, which can't be returned from JavaScript.
*/
$response= $m->test->execute("return db.foo.find().toArray();");
print_r($response); //[$id] => 5287cd2260e2eac9a0e2f1ca ) [name] => happy [age] => 18 ) [2] => Array ( [_id] => MongoId Object ( [$id] => 5287cd2260e2eac9a0e2f1cb ) [name] => prince [age] => 16 ) [3] => Array ( [_id] => MongoId Object ( [$id] => 5287cdea60e2eac9a0e2f1cc ) [name] => nanhe [age] => 30 ) [4] => Array ( [_id] => MongoId Object ( [$id] => 5287cdea60e2eac9a0e2f1cd ) [name] => happy [age] => 18 ) [5] => Array ( [_id] => MongoId Object ( [$id] => 5287cdea60e2eac9a0e2f1ce ) [name] => prince [age] => 16 ) ) [ok] => 1 )

$response= $m->test->execute("return db.foo.find({'name':'nanhe'}).toArray();");
print_r($response); //Array ( [retval] => Array ( [0] => Array ( [_id] => MongoId Object ( [$id] => 5287ce9b60e2eac9a0e2f1d2 ) [name] => nanhe [age] => 30 ) ) [ok] => 1 )
// $id value will be different in your case