Pages

Tuesday, December 11, 2012

More on PHP Programming

1. Finding the variable name by value

<?php
function var_name(&$var, $scope=0)
{
    $key = array_search($var, $GLOBALS);
    return $key;
}
$name = 'samy';
echo var_name($
name);
?>

 O/P

name

Above code will works for only  unique variable used for the page. finding the variable name by value with multiple variable name in a same page use following function

<?php
echo vname($inp);
function vname(&$var, $scope=false, $prefix='unique', $suffix='value')
  {
    if($scope) $vals = $scope;
    else      $vals = $GLOBALS;
    $old = $var;
    $var = $new = $prefix.rand().$suffix;
    $vname = FALSE;
    foreach($vals as $key => $val) {
      if($val === $new) $vname = $key;
    }
    $var = $old;
    return $vname;
  }
?>


 O/P
name

Monday, December 10, 2012

Getting remote address

function getClientIP () {

     if (isset ($_SERVER ['HTTP_X_FORWARDED_FOR'])){
                 $clientIP = $_SERVER ['HTTP_X_FORWARDED_FOR'];
     }elseif (isset ($_SERVER ['HTTP_X_REAL_IP'])){
                 $clientIP = $_SERVER ['HTTP_X_REAL_IP'];
     }else {
                 $clientIP = $_SERVER['REMOTE_ADDR'];
     }
     return $clientIP;


}

Also we can use following API method

 <?php
echo getClientIp();

    function getClientIp()
    {
        $env = 'prod';
        if($env == 'prod' ){
      
            $url = "http://jsonip.com/";
            $curlData =Ipcurl($url);
            $sttus = explode("||",$curlData);
            if($sttus[1]==200)
                $xmlData_ip = $sttus[0];
    
            $ipPasrse    = explode(",",$xmlData_ip);
          
            if($ipPasrse[0]!="")
            $ipGet         = explode(":",$ipPasrse[0]);
          
            if($ipGet[1]!="")
            $ipAddress    = str_replace('"',"",$ipGet[1]);
            
        }
        return $ipAddress ;
    }
  
    function Ipcurl( $url, $cookiefile = '', $pmSSL = FALSE, $pmCert = FALSE)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);                                                  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
        if($pmSSL == TRUE){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,$pmCert); // You should be able to set this to TRUE if your SSL certificate
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,TRUE);
        }
        $result = curl_exec($ch);
        $info = curl_getinfo ($ch);
        if (curl_errno($ch)) {
                $error = curl_error($ch);
        }
            curl_close($ch);  
        return $result.'||'.$info['http_code'];

    }
?>

Wednesday, December 5, 2012

Zend framework for beginners

1. Installation(For windows)

Step-1

The First Basic step is to download the Zend Framework Package itself. You can Download the Package from here. Please make sure this should be a full package

Step-2


The next part is including the Zend Library in php.ini file. You can find the php.ini file in C:\xampp\php\php.ini.  Open it with the code editor and find this line which is usually at line no 794.
include_path = ".;D:\xampp\php\PEAR”
Now include the path where you have extracted the Zend Framework, so the above line changes like below
include_path = ".;D:\xampp\php\PEAR;D:\xampp\htdocs\samy\ZendFramework-1.12.0\library"
and Close the php.ini file.

Note: If you done this, then no need to set the dynamic include path at application's index.php file

 Step-3
 Needs to set the environment variable for notifying the bin installers(zf.sh,zf.bat)Create either a System or User Environment Variable with the following
 variable-1
Variable Name: ZEND_TOOL_INCLUDE_PATH
Variable Value: D:\xampp\htdocs\samy\ZendFramework-1.12.0\library
 variable-2
Variable Name: PATH
Variable Value: D:\xampp\htdocs\samy\ZendFramework-1.12.0\bin;D:\xampp\php
 You can set the environment variables in windows using following way
start>mycomputer>right click>properties>advanced system settings>Environment variables>

here we can add/edit the variables
if variable name is already exist then apend the values with ";"

Step -4

After setting the paths restart the system and access the command prompt

start>cmd
c:>
D:\xampp\htdocs\samy> php -v
 It will display the php version

D:\xampp\htdocs\samy> zf show version
 It will display the zend framework version

D:\xampp\htdocs\samy> zf create project zend_demo
 It should create the new project with folder structure, you can see the folders at below path

 D:\xampp\htdocs\samy\zend_demo
In this folder structure "library folder" doesn't have any files/folders inside it
so copy the full library files from downloaded package( click here to download) and paste it here

Creating a basic Zend project using CLI Tool/command prompt in Windows

Step-1

The First Basic step is to download the Zend Framework Package itself. You can Download the Package from here. Please make sure this should be a full package

Step-2

The next part is including the Zend Library in php.ini file. You can find the php.ini file in C:\xampp\php\php.ini.  Open it with the code editor and find this line which is usually at line no 794.
include_path = ".;D:\xampp\php\PEAR”
Now include the path where you have extracted the Zend Framework, so the above line changes like below
include_path = ".;D:\xampp\php\PEAR;D:\xampp\htdocs\samy\ZendFramework-1.12.0\library"
and Close the php.ini file.
Note: If you done this, then no need to set the dynamic include path at application's index.php file
 Step-3
 Needs to set the environment variable for notifying the bin installers(zf.sh,zf.bat)Create either a System or User Environment Variable with the following
 variable-1
Variable Name: ZEND_TOOL_INCLUDE_PATH
Variable Value: D:\xampp\htdocs\samy\ZendFramework-1.12.0\library
 variable-2
Variable Name: PATH
Variable Value: D:\xampp\htdocs\samy\ZendFramework-1.12.0\bin;D:\xampp\php
 You can set the environment variables in windows using following way
start>mycomputer>right click>properties>advanced system settings>Environment variables>

here we can add/edit the variables
if variable name is already exist then apend the values with ";"

Step -4

After setting the paths restart the system and access the command prompt

start>cmd
c:>
D:\xampp\htdocs\samy> php -v
 It will display the php version

D:\xampp\htdocs\samy> zf show version
 It will display the zend framework version

D:\xampp\htdocs\samy> zf create project zend_demo
 It should create the new project with folder structure, you can see the folders at below path

 D:\xampp\htdocs\samy\zend_demo
In this folder structure "library folder" doesn't have any files/folders inside it
so copy the full library files from downloaded package( click here to download) and paste it here



 





Friday, November 9, 2012

Magento - Creating custom modules and blocks

directory structure:
/app/code/local/<Namespace>/<Module>/

Block/
controllers/
etc/
Model/
    Mysql4/
        <Module>/
sql/
    <module>_setup/
 
 /app/design/frontend/<interface>/<theme>/
template/
    <module>/


Lets assume following values

 <Namespace> = Test

 <Module>    = Tutorial

 <interface> =  green

 <theme>     = default

1. create module

Magento requires there to be an XML file that tells Magento to look for and use your custom module.
/app/etc/modules/<Namespace>_<Module>.xml

<?xml version="1.0"?>
<config>
    <modules>
        <[Namespace]_[Module]>
            <active>true</active>
            <codePool>local</codePool>
        </[Namespace]_[Module]>
    </modules>
</config>
 
 
Replaced code
<?xml version="1.0"?>
<config>
    <modules>
        <Test_Tutorial>
            <active>true</active>
            <codePool>local</codePool>
        </Test_Tutorial>
    </modules>
</config>
 
 
2. Create controller
 
/app/code/local/<Namespace>/<Module>/controllers/IndexController.php
 
<?php
class <Namespace>_<Module>_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
            $this->loadLayout();
            $this->renderLayout();
    }
}
Replaced code  
 <?php
class Test_Tutorial_IndexController extends Mage_Core_Controller_Front_Action
{  
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();   
    } 
  
}

 
3. Create configuration XML

/app/code/local/<Namespace>/<Module>/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <[Namespace]_[Module]>
            <version>0.1.0</version>
        </[Namespace]_[Module]>
    </modules>
    <frontend>
        <routers>
            <[module]>
                <use>standard</use>
                <args>
                    <module>[Namespace]_[Module]</module>
                    <frontName>[module]</frontName>
                </args>
            </[module]>
        </routers>
        <layout>
            <updates>
                <[module]>
                    <file>[module].xml</file>
                </[module]>
            </updates>
        </layout>
    </frontend>   
   

        <blocks>
            <[module]>
                <class>[Namespace]_[Module]_Block</class>
            </[module]>
        </blocks>

</config>

Replaced code

<?xml version="1.0"?>
<config>
    
    <modules>
        <Test_Tutorial>
        <version>0.1.0</version>
        </Test_Tutorial>
    </modules>

    <frontend>
        <routers>
            <tutorial>
                <use>standard</use>
                <args>
                    <module>Test_Tutorial</module>
                    <frontName>tutorial</frontName>
                </args>
            </tutorial>
        </routers>
   
        <layout>
            <updates>
                <tutorial>
                    <file>tutorial.xml</file>
                </tutorial>
            </updates>
        </layout>
    </frontend>
  
   <global>
        <blocks>
            <tutorial>
                    <class>Test_Tutorial_Block</class>
            </tutorial>
        </blocks>
    </global>
</config>


 4. Template design
/app/design/frontend/<interface>/<theme>/layout/<module>.xml 

<?xml version="1.0"?>
<layout version="0.1.0">
    <[module]_index_index>
        <reference name="content">
            <block type="core/template" name="[module]" template="[module]/[module].phtml" />
        </reference>
    </[module]_index_index>
</layout>

 Replaced code
 <?xml version="1.0"?>
<layout version="0.1.0">
 <default>
      <reference name="content">
      </reference>
  </default>
 
  <tutorial_index_index>
    <reference name="content">
      <block type="tutorial/list" name="tutorial" template="tutorial/tutorial.phtml" />
    </reference>
  </tutorial_index_index>

</layout>


5. Creating block
/app/code/local/<Namespace>/<Module>/Block/List.php

<?php
class
<Namespace>_<Module>_Block_List extends Mage_Core_Block_Template
{
    public function _prepareLayout()
    {       
        return parent::_prepareLayout();
    }

   //methods will goes on

}


Replaced code 

<?php
class Test_Tutorial_Block_List extends Mage_Core_Block_Template
{
    public function _prepareLayout()
    {       
        return parent::_prepareLayout();
    }

    public function myMessage(){
        return "This is test message, This is my module.";
    }

}

 
6. create template file

/app/design/frontend/<interface>/<theme>/template/<module>/<module>.phtml

<?php  $this->methidNameBlock() ;?>

Replaced code
 
 <div>
    <?php
        echo 'Testing message';
        echo "<BR/>";
        echo $this->myMessage();
    ?>
</div>



 7. Check your output

http://localhost/magento_1.6/tutorial/index









 

 

Wednesday, October 17, 2012

Dynamic tabs using jQuery - why and how to create it

This tutorial will show you how to use jQuery to create tabs that can be added and removed dynamically. Although the example looks bulletproof, there are some questions I will raise about how to use tabs and in which context should they be used in order to make them meaningful and usable.
View demo
When we talk about tabs on web we usually think about tabs used for navigation. However, in this case I'd like to use them in a different context. I want to use them for showing different entities on the same page. By entities I mean anything from plain text to web forms. Actually, I will try to simulate tabs as seen in browsers. Well, at least to some extent. Take Google docs for example, instead of opening each document in a new window (or browser tab), you could open them in page tabs.


Refere following link for more details

http://www.jankoatwarpspeed.com/post/2010/01/26/dynamic-tabs-jquery.aspx