Monday, 25 February 2013

Detecting AJAX Requests in Zend Framework 1


In Zend Framework 1, the Zend Controller Request Object (Zend_Controller_Request_Http) has a method for detecting AJAX requests: isXmlHttpRequest().

This method looks for an HTTP request header X-Requested-With with the value 'XMLHttpRequest'; if found, it returns TRUE. otherwise it return FALSE.

In any controller action, if you need to check whether the HTTP Request as 'AJAX', here is the code

<?php

class UserController extends Zend_Controller_Action {
  public function loaddataAction(){
   if($this->_request->isXmlHttpRequest()){
    //ajax call request
   }
  }
}

?>
Hope you enjoyed this Post.

Sunday, 24 February 2013

Add attributes to SimpleXML in PHP


We have already seen how to create basic XML and Nested XML using SimpleXML. Today we are going to see how to add attributes to simpleXML Element using addAttribute().

This addAttribute() accepts 3 Parameters:

1) Name : The name of the attribute to add.
2) Value : The value of the attribute.
3) Namespace: If specified, the namespace to which the attribute belongs.

And it doesn't return any values.

We will see, how to add type attribute to user element as shown below

<?xml version="1.0" encoding="UTF-8"?>
<users>
<user type="single">
<name id="100">suresh</name>
<user>
</users

First, we will create simpleXML Object with root element (users) using SimpleXMLElement().
Then using addchild(), we will add child element (user)  to root element (users), this method will return reference of child element added.

<?php
$users = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?></users>');
$user = $users->addChild('user');
?>

We also need to add another child element 'name' to 'user' element.

<?php
$name = $user->addChild('name','suresh');
?>

Now $user will contain reference of child element 'user' and $name will contain reference of child element 'name'.

We will add both type and id attributes to both child elements (user and name) using addAttribute().

<?php
$user->addAttribute('type','single');
$name->addAttribute('id','100');
?>

Here is the combined code for adding attributes

<?php
$users = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><users/>');
$user = $users->addChild('user');
$name = $user->addChild('name','suresh');
$user->addAttribute('type','single');
$name->addAttribute('id','100');
header('Content-Type: text/xml');
echo $users->asXML();
?>

Hope, you enjoyed this Post.

Thursday, 21 February 2013

Create Nested XML using SimpleXML in PHP


If you are not familiar with creating Basic XML using simpleXML. First read this Post. Today, we are going to see how to create Nested XML as shown below using SimpleXML.

<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
    <begineer>
       <courses>
          <course>Php</course>
          <course>Ajax</course>
       </courses>
     </begineer>
     <advanced>
<courses>
           <course>Java</course>
  <course>J2EE</course>
</courses>
     </advanced>
</user>
</users>

Create a new simpleXML object with root element (users) using SimpleXMLElement()

<?php
$users = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?></users>');
?>

Add child element (user) to the root element (users) using addChild(). This addChild() will return the reference of the child element added.

<?php
$user = $users->addChild('user');
?>

There are 2 child elements (begineer and advanced) present under 'user' element and these needed to be added using addChild().

<?php
$begineer = $user->addChild('begineer');
$advanced = $user->addChild('advanced');
?>

Now $begineer will contain reference of 'begineer' element and $advanced will contain reference of 'advanced' element.
We need to add 'courses' child element to both 'begineer' and 'advanced' element.

<?php
$begineercourses = $begineer->addChild('courses');
$advancedcourses = $advanced->addChild('courses');
?>

We need to add 'course' child element to both begineer and advanced courses.

<?php
$begineercourses->addChild('course','Php');
$begineercourses->addChild('course','Ajax');

$advancedcourses->addChild('course','Java');
$advancedcourses->addChild('course','J2EE');
?>

use asXML(), which will convert SimpleXML Object to well-formed XML string and output to browser.
you can use header() function with 'Content-Type:text/xml' which will tell the browser that we are going to output XML data.

<?php
header('Content-Type: text/xml');
echo $users->asXML();
?>

Following is the combined code for creating Nested XML using SimpleXML

<?php
$users = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?></users>');
$user = $users->addChild('user');
$begineer = $user->addChild('begineer');
$advanced = $user->addChild('advanced');
$begineercourses = $begineer->addChild('courses');
$advancedcourses = $advanced->addChild('courses');
$begineercourses->addChild('course','Php');
$begineercourses->addChild('course','Ajax');

$advancedcourses->addChild('course','Java');
$advancedcourses->addChild('course','J2EE');

header('Content-Type: text/xml');
echo $users->asXML();
?>

I hope you enjoyed this Post.

create XML using SimpleXML in PHP


We will see how to create XML using simpleXML. we will creating the below XML

<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
 <id>2011</id>
 <name>A Suresh</name>
 <age>29</age>
 <role>Senior Developer</role>
</user>
</users>

First, Create simpleXML object with root element (users) using SimpleXMLElement()

<?php
$users = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><users/>');
?>

then add child element (user) to the root element (users) using addChild(), this will return the reference of the child element added.

<?php
$user = $users->addChild('user');
?>

now $user will contain reference of child element added (user), we will add other child elements with values to user.

<?php
$user->addChild('id',2011);
$user->addChild('name','A Suresh');
$user->addChild('age',29);
$user->addChild('role','Senior Developer');
?>

use asXML(), which will convert SimpleXML Object to well-formed XML string and output to browser.
you can use header() function with Content-Type:text/xml which will tell the browser that we are going to output XML data.

<?php
header('Content-Type: text/xml');
echo $users->asXML();
?>

Following is the combined code for creating XML using SimpleXML

<?php
$users = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><users/>');
$user = $users->addChild('user');
$user->addChild('id',2011);
$user->addChild('name','A Suresh');
$user->addChild('age',29);
$user->addChild('role','Senior Developer');
header('Content-Type: text/xml');
echo $users->asXML();
?>

I hope you enjoyed this Post.

Wednesday, 20 February 2013

Access view helper inside another helper in Zend Framework 1

Today, we are going to see the right way to access view helper inside another view helper in Zend Framework 1.

If you need to access any specific method inside any class, we will create an instance of that class and access the method, this same approach is getting followed when you need to access any view helper inside another vew helper. (Please don't follow this approach)
<?php
    $userHelper = new View_Helper_User();
    $userHelper->getUserData();
?>
But Zend framework 1, gives you the view object inside view helper and using that view object you can access other view helper. if you need to access any view helper (user), this below code will work
<?php
   $this->view->user($userId);
?>

If you need to access any method (getUserData) inside the user view helper, this below code will work
<?php
    $this->view->getHelper('user')->getUserData($article);
?>

Hope you enjoyed this post and let me know any feedback or better approach.


Monday, 18 February 2013

Access action helper from another helper in Zend Framework 1

In Zend Framework 1, i have seen lot of people accessing action helper inside another helper by following these two approaches

Approach 1:

a) Create an instance of the controller helper class
b) call the method of the class using object

<?php

$helperObj = new Controller_Action_Helper_Test()
$helperObj->direct();

?>

Approach 2:

<?php
//controller helper class name
$helperObj = $this->getActionController()->getHelper('Test');  
$helperObj->direct();
?>
I am not here to argue the above two approaches are not correct. Both will work as expected
.I am saying this is not the right way to access it.  Below is the right way to access action helper inside another helper.

<?php
$helperObj = Zend_Controller_Action_HelperBroker::getStaticHelper('Test')->direct();
?>
Hope, you enjoyed this post. Did you have any other better approach?

Array Creation in PHP

In PHP, we are going to see the variouse ways for creating an array.

Option 1:
$firstArray = array();
$firstArray[] = ‘id’;
$firstArray[] = ‘name’;
$firstArray[] = ‘city’;

Option 2:
$firstArray = array(‘id’,'name’,'city’);

Option 3: // as of PHP 5.4
$firstArray = ['id','name','city'];


Please let me know, if there is any other ways available for creating PHP arrays?

Hi To All..

Hi to All,
           I am very excited to start my new journey as blogger.   I am a regular reader of PHP Blogs. I like to share my PHP knowledge to others here. I hope you will enjoy my blog posts. Hope to see you soon with my first post.
Related Posts Plugin for WordPress, Blogger...