Thursday, June 30, 2016

Parsing Complex XML with SimpleXML in PHP

Let's parse a very complex XML Data using SimpleXML methods in PHP. Let's take a complex XML data as shown below.

<NodeLevel1>
 <NodeLevel2>
  <NodeLevel3>
<RaceDay RaceDayDate="2016-06-29" >
   <Meeting MeetingCode="BR" MtgId="1299709952" VenueName="Doomben" >
<Pool PoolType="DD" DisplayStatus="SELLING"></Pool>
<Pool PoolType="XD" DisplayStatus="PAYING"></Pool>
<Pool PoolType="TT" DisplayStatus="CLOSED"></Pool>
<Pool PoolType="QD" DisplayStatus="CLOSED"></Pool>
<MultiPool PoolType="XD" DisplayStatus="PAYING"></MultiPool>
<Race RaceNo="1" RaceTime="12:53" RaceName="2YO HANDICAP" />
<Race RaceNo="2" RaceTime="13:23" RaceName="BM 75 HANDICAP" />
<Race RaceNo="3" RaceTime="13:53" RaceName="MAIDEN PLATE" >
     <TipsterTip TipsterId="0" Tips="4"/>
     <TipsterTip TipsterId="5" Tips="1-9-4-8"/>
     <Pool PoolType="A2" Available="Y" Abandoned="N" />
     <Pool PoolType="EX" Available="Y" Abandoned="N" />
     <Pool PoolType="F4" Available="Y" Abandoned="N" />
     <Runner RunnerNo="1" RunnerName="ALL TROOPS" />
     <Runner RunnerNo="2" RunnerName="SEQ THE STAR" />
     <Runner RunnerNo="3" RunnerName="SHADOW LAWN"/>
     <Runner RunnerNo="4" RunnerName="FREQUENDLY" />
</Race>
<Tipster TipsterId="0" TipsterName="LATE MAIL"/>
<Tipster TipsterId="1" TipsterName="RADIO TAB"/>
<Tipster TipsterId="2" TipsterName="TRACKMAN"/>
 </Meeting>
</RaceDay>
<RaceDay RaceDayDate="2016-06-30" >
 <Meeting MeetingCode="MR" MtgId="2299719559" VenueName="Lucas" >
<Pool PoolType="CC" DisplayStatus="SELLING"></Pool>
<Pool PoolType="YD" DisplayStatus="PAYING"></Pool>
<Pool PoolType="VT"  DisplayStatus="CLOSED"></Pool>
<Pool PoolType="MD" DisplayStatus="CLOSED"></Pool>
<MultiPool PoolType="VD" PoolDisplayStatus="PAYING"></MultiPool>
<Race RaceNo="1" RaceTime="12:53" RaceName="R2YO BHANDI" />
<Race RaceNo="2" RaceTime="13:23" RaceName="XX 75 ZINDA" />
<Race RaceNo="3" RaceTime="13:53" RaceName="PLATE RAIDEN" >
    <TipsterTip TipsterId="0" Tips="5"/>
    <TipsterTip TipsterId="5" Tips="2-1-4-8-4"/>
    <Pool PoolType="A2" Available="Y" Abandoned="N" />
    <Pool PoolType="EX" Available="Y" Abandoned="N" />
    <Pool PoolType="F4" Available="Y" Abandoned="N" />
    <Runner RunnerNo="1" RunnerName="ALL BROOKS" />
    <Runner RunnerNo="2" RunnerName="MIDDLE STAR" />
    <Runner RunnerNo="3" RunnerName="LONELY LAWN"/>
    <Runner RunnerNo="4" RunnerName="OBLIV" />
</Race>
<Tipster TipsterId="0" TipsterName="EARLY MAIL"/>
<Tipster TipsterId="1" TipsterName="RADIO CAB"/>
<Tipster TipsterId="2" TipsterName="JACKMAN"/>
 </Meeting>
  </RaceDay>
 </NodeLevel3>
</NodeLevel2>
</NodeLevel1>  

See that <NodeLevel3> node has two <RaceDay> nodes in it. And each <RaceDay> has its own <Meeting> node. Again each <Meeting> node  has various nodes like <Pool>, <MultiPool>, <Race>, <Tipster> as its children. Finally each <Race> node has <TipsterTip>, <Pool> and <Runner> nodes under it.

Here, Most of the nodes have attributes and some have descendants under it. We would traverse through all the <RaceDay> nodes and finds all its attributes and children. Let's start it.

<?php
$xml_source = <<<EOD
<NodeLevel1>
 <NodeLevel2>
  <NodeLevel3>
   <RaceDay RaceDayDate="2016-06-29" >
     <Meeting MeetingCode="BR" MtgId="1299709952" VenueName="Doomben" >
      <Pool PoolType="DD" DisplayStatus="SELLING"></Pool>
      <Pool PoolType="XD" DisplayStatus="PAYING"></Pool>
      <Pool PoolType="TT" DisplayStatus="CLOSED"></Pool>
      <Pool PoolType="QD" DisplayStatus="CLOSED"></Pool>
      <MultiPool PoolType="XD" DisplayStatus="PAYING">
      </MultiPool>
      <Race RaceNo="1" RaceTime="12:53" RaceName="2YO HANDICAP"/>
      <Race RaceNo="2" RaceTime="13:23" RaceName="BM7 HANDICAP"/>
      <Race RaceNo="3" RaceTime="13:53" RaceName="MAIDEN PLATE">
       <TipsterTip TipsterId="0" Tips="4"/>
       <TipsterTip TipsterId="5" Tips="1-9-4-8"/>
       <Pool PoolType="A2" Available="Y" Abandoned="N" />
       <Pool PoolType="EX" Available="Y" Abandoned="N" />
       <Pool PoolType="F4" Available="Y" Abandoned="N" />
       <Runner RunnerNo="1" RunnerName="ALL TROOPS" />
       <Runner RunnerNo="2" RunnerName="SEQ THE STAR" />
       <Runner RunnerNo="3" RunnerName="SHADOW LAWN"/>
       <Runner RunnerNo="4" RunnerName="FREQUENDLY" />
     </Race>
     <Tipster TipsterId="0" TipsterName="LATE MAIL"/>
     <Tipster TipsterId="1" TipsterName="RADIO TAB"/>
     <Tipster TipsterId="2" TipsterName="TRACKMAN"/>
   </Meeting>
  </RaceDay>
  <RaceDay RaceDayDate="2016-06-30" >
   <Meeting MeetingCode="MR" MtgId="2299719559" VenueName="Las" >
    <Pool PoolType="CC" PoolDisplayStatus="SELLING"></Pool>
    <Pool PoolType="YD" PoolDisplayStatus="PAYING"></Pool>
    <Pool PoolType="VT" PoolDisplayStatus="CLOSED"></Pool>
    <Pool PoolType="MD" PoolDisplayStatus="CLOSED"></Pool>
    <MultiPool PoolType="VD" DisplayStatus="PAYING">
    </MultiPool>
    <Race RaceNo="1" RaceTime="12:53" RaceName="R2YO BHANDI" />
    <Race RaceNo="2" RaceTime="13:23" RaceName="XX 75 ZINDA" />
    <Race RaceNo="3" RaceTime="13:53" RaceName="PLATE RAIDEN" >
     <TipsterTip TipsterId="0" Tips="5"/>
     <TipsterTip TipsterId="5" Tips="2-1-4-8-4"/>
     <Pool PoolType="A2" Available="Y" Abandoned="N" />
     <Pool PoolType="EX" Available="Y" Abandoned="N" />
     <Pool PoolType="F4" Available="Y" Abandoned="N" />
     <Runner RunnerNo="1" RunnerName="ALL BROOKS" />
     <Runner RunnerNo="2" RunnerName="MIDDLE STAR" />
     <Runner RunnerNo="3" RunnerName="LONELY LAWN"/>
     <Runner RunnerNo="4" RunnerName="OBLIV" />
    </Race>
    <Tipster TipsterId="0" TipsterName="EARLY MAIL"/>
    <Tipster TipsterId="1" TipsterName="RADIO CAB"/>
    <Tipster TipsterId="2" TipsterName="JACKMAN"/>
  </Meeting>
 </RaceDay>
</NodeLevel3>
</NodeLevel2>
</NodeLevel1>
EOD;
?>

See how I have declared the XML in a string using Heredoc in PHP.

$xml_source = <<<EOD

When using Heredoc, we need to make sure that there is no blankspace after the opening identifier. So, "<<<EOD" must be followed by a newline "\n"; which means in the editor, after typing "<<<EOD" we need to press ENTER to move to the new line.

Heredoc helps us to avoid quote (' or ") usage problems. See, all the node attributes are wrapped in double quote. We have another method to define the XML string as shown below. 

/// We make sure that all single quotes are escaped
$xml_source = '<NodeLevel1><NodeLevel2><NodeLevel3>' .
              '<RaceDay Name="John O\'Neal" > ..... '; 
 
Ok, now let's proceed.

// LOAD the XML Root Object
$all_nodes = new SimpleXMLElement($xml_source);

// BROWSE to Certain PATH/NODE
$all_nodelevel3 = $all_nodes
                  ->xpath('/NodeLevel1/NodeLevel2/NodeLevel3');

// PRINT what we got
print_r($all_nodelevel3);

The above piece of code would load the XML data, create SimpleXMLElement Object with it. Then we are traversing to "/NodeLevel1/NodeLevel2/NodeLevel3" node in the XML tree. xpath method actually searches the SimpleXML node for children matching the XPATH provided as its argument. We don't add the trailing slash ('/') to the end of our XPATH. 

To get all the <NodeLevel1> we need to pass "/NodeLevel1" as argument to xpath() method.

Now, let's print all the <Runner> nodes in the above XML.

<?php
// LOAD the XML Root Object
$all_nodes = new SimpleXMLElement($xml_source);

// BROWSE to all <NodeLevel3>
$all_nodelevel3 = $all_nodes->xpath('/NodeLevel1/NodeLevel2/NodeLevel3');

// LOOP THRU <NodeLevel3> nodes
foreach($all_nodelevel3 as $nodelevel3)
{
  // GEt All <RaceDay> Nodes
  $all_racedays = $nodelevel3->RaceDay;
  
  // LOOP THRU All <RaceDay> Nodes
  foreach($all_racedays as $raceday)  
  {
// GET ALL <Meeting>
$all_meeting = $raceday->Meeting;
 
// Loop Thru <Meeting>
foreach($all_meeting as $meeting)
{
     // GET ALL RACE
     $all_race = $meeting->Race;

     // LOOP Thru <Race> Nodes
     foreach($all_race as $race)
     {
 
       // GET ALL <Runner> nodes
       $all_runners = $race->Runner;
 
       /// Note that some <Race> nodes don't have
       /// <Runner> nodes under it
       /// So, we check if <Runner> nodes exist
       if($all_runners)
       {
/// Loop Thru <Runner> nodes
foreach($all_runners as $runner)
{
          /// GEt <Runner> Node's attributes
          $atts = $runner->attributes();

          // Loop Thru Attributes
          $str = "";
          foreach($atts as $key => $val)
          {
$str .= "$key => $val, ";
          }
         // PRINT  
          echo "RUNNER  :: $str <br>";

}
       }
     }
}
 }
}
?>

Check the Output Below :: 

RUNNER :: RunnerNo => 1, RunnerName => ALL TROOPS, 
RUNNER :: RunnerNo => 2, RunnerName => SEQ THE STAR, 
RUNNER :: RunnerNo => 3, RunnerName => SHADOW LAWN, 
RUNNER :: RunnerNo => 4, RunnerName => FREQUENDLY, 
RUNNER :: RunnerNo => 1, RunnerName => ALL BROOKS, 
RUNNER :: RunnerNo => 2, RunnerName => MIDDLE STAR, 
RUNNER :: RunnerNo => 3, RunnerName => LONELY LAWN, 
RUNNER :: RunnerNo => 4, RunnerName => OBLIV, 

See, how we have used "foreach" loop structure to traverse nodes and get deeper into the XML Tree. foreach construct has been used considering that <RaceDay>, <Meeting>, <Race> and <Runner> nodes may appear in any number within their Parent Node in the XML Tree. 

We even used foreach($all_nodelevel3 as $nodelevel3) to consider that many <NodeLevel3> nodes co-exist within a single <NodeLevel2> node.

Secondly, we have used attributes() function to get all the attributes of a node.

Hope this helps.

Tuesday, May 17, 2016

Determine Page Type in Magento

If we want to know page type ( whether it is a Category, Product, CMS, Cart page or Checkout Success page ) in Magento, the following code may come handy.

<?php

// GET current Category, Product
$category = Mage::registry('current_category');         
$product  = Mage::registry('current_product');

// WE May use the below also
//$product  = Mage::registry('product');

// GET ROUTE information
$route =  Mage::app()->getFrontController()
                     ->getRequest()->getRouteName();

// Return controller name
$controller = Mage::app()->getRequest()
                         ->getControllerName(); 

// Return action name
$action = Mage::app()->getRequest()->getActionName(); 


if($product != null) 
{
   // PRODUCT PAGE
   // WRITE code for PRODUCT PAGE
   
}
else if($category != null) 
{
   // CATEGORY PAGE
   // WRITE code for CATEGORY PAGE
}
else 
{  
   // CMS PAges
   $identifier = Mage::getSingleton('cms/page')->getIdentifier();
   
   // CART PAGE and ORDER SUCCESS Page
   if($route == 'checkout')
   {
      // Success Page
      if($action == 'success')
      {
// GET ORDER TOTAL
$orderId = Mage::getSingleton('checkout/session')
                    ->getLastRealOrderId();
        $order = Mage::getSingleton('sales/order')
                    ->loadByIncrementId($orderId);
        $orderTotal = $order->getGrandTotal(); 
      }
      else 
      // CART PAge
      {

// GET Cart Total
$quote = Mage::getModel('checkout/session')
                               ->getQuote();
$quoteData= $quote->getData();
$cartTotal = $quoteData['grand_total'];
      }
   
   }
   
   // FIRECHECKOUT PAGE
   if($route == 'firecheckout')
   {
                // IF IT is FIRECHECKOUT Page
   }
   
   // Search Result PAGE
   if($route == 'catalogsearch')
   {
       // IF IT is SEARCH RESULT Page
   }
   
   // Website HOME page
   if($identifier == "home")
   {
      // IF it is HOME PAGE  
   }
}
?>


So, In the first section of the program, we are trying to get if it is a CATEGORY or PRODUCT page by calling 

$category = Mage::registry('current_category');         
$product  = Mage::registry('current_product');

We may use  $product = Mage::registry('product'); instead of $product = Mage::registry('current_product'); 

IF it is a Category page, $category would have a non-null value. Similarly, when it is a product page, $product variable would have a non-null value. 

To determine CMS and other pages like Cart, Checkout etc, we need to catch the route, controller name and action name. So we use the following ::

$route =  Mage::app()->getFrontController()
                     ->getRequest()->getRouteName();
$controller = Mage::app()->getRequest()->getControllerName(); 
$action = Mage::app()->getRequest()->getActionName(); 

For CMS pages, we get page ID/Identifier by calling this :: 
$identifier = Mage::getSingleton('cms/page')->getIdentifier();

There is another Magento fundtion called getFullActionName() which returns full action name which comprises of route name, controller name and action name. 

$fullActionName = Mage::app()->getFrontController()
                             ->getAction()
                             ->getFullActionName();

So, on "Contacts" page, full action name is 'contacts_index_index' where 'contacts' is the front name, first 'index' means 'indexController' controller and second 'index' refers to 'indexAction' function/method defined within that controller PHP file.

Some full action name examples given below. From there, we can easily identify the Front/Route name, Controller Name and Action Name.

checkout_cart_index                => Cart Page
checkout_onepage_index         => Checkout Page
checkout_onepage_success    => Checkout Success Page
customer_account_login         => Customer Login
customer_account_logoutSuccess => Customer Logout 
customer_account_create      => Registration Page
customer_account_index        => Customer Dashboard
wishlist_index_index           => My Wishlist
cms_page_view                              => any CMS page
contacts_index_index             => Contacts page 
catalog_seo_sitemap_category  => SiteMap Catalog
catalogsearch_term_popular        => Search Term Page
catalogsearch_advanced_index => Advanced Search

Hope this helps.

Tuesday, April 05, 2016

PHP and MongoDB - I

In our previous Article, we had seen some basic commands of MongoDB in action. Now, we would connect our PHP to MongoDB. 

We already know how to install MongoDB driver for PHP and how to install MongoDB as Windows Services. Next in our PHP file, we start connecting to MongoDB as shown below.

<?php
ini_set('display_errors', 1);

// Let's connect without Authorization
$mongo = new MongoClient();

// GET Connections
$mongoConnections = $mongo->getConnections();

// IF Connection Found
if (false === empty($mongoConnections)) 

   // LIST Databases
   $dbs = $mongo->listDBs();
   
   // Show List of DataBases
   echo "<b>Showing All the DB Names :</b> ";
   foreach($dbs['databases'] as $d)
   {
     $dn = $d['name'];
     echo "<br>Database Name : $dn" ;
 
     // Select the database
     $p = $mongo->{$dn};
 
     // Get all the Collections
     $c = $p->listCollections();
     echo ", Collections are : ";
     foreach($c as $n) 
     {
       echo "[$n] ";
     } 
   }
   
   // Select our Database within the 1
   // position within the $dbs array
   $db = $dbs['databases'][1]['name'];
   $db = $mongo->{$db};
   
   // Select a collection say "myDatabase"
   $col = $db->myDatabase;
   
   // get all records
   $records = $col->find();
   
   // Iterate and show
   foreach($records as $key=>$val)
   {
     echo "<br>First Name : " . $val['fname'] . 
          ", Last Name : " . $val['lname'] . 
          ", Roll : " . $val['roll'] . 
          ", Status : " . $val['status']  ;
   }
   
   // INSERT a new ROW in that Collection
   // Insert a Document to Collection
   $arr =  array('fname' => 'Amiya', 'lname' => 'Sengupta', 
                 'roll' => '33', 'status' => '4');
   $col->insert($arr);
   
   
   // If we want to create a New Collection, JUST
   // use its name, Mongo will automatically create it
   $col = $db->PassedStudents;
   
   // Insert a Document to Collection
   $arr =  array('fname' => 'Adhuna', 'lname' => 'Muland', 
                 'roll' => '10', 'status' => '3');
   $result = $col->insert($arr);
   
   // Check if INSERT was successful
   if($result['ok'] != 1)
    echo " INSERT ERROR";
   
   // CREATE A NEW COLLECTION called 'Publisher'
   $publisher = $db->createCollection(
      "publisher",
       array(
        'capped' => true,
        'size' => 10*1024,
        'max' => 10
      )
   );
   
   // Insert some records in Publisher
   $arr =  array(
           'name' => 'General Publisher', 
           'address' => '10, Peter Road, Zip 2343', 
           'phone' => '9876543210');
   $result = $publisher->insert($arr);   
   

else 
{
   // Not connected
   echo "MongoDB not connected";
}
?>

The above code is quite self-explanatory. We have the MongoClient object available with us. This object helps us to interact with MongoDb database.

Some points which need to be noticed here are : 
1. listDBs() gets all the Database available. This is equivalent to "show dbs" command in Mongo Shell.
2. listCollections() gets all the collections available within the database. 
3. find() is used to find all records within a collection. The find() method returns a MongoCursor object 
4. insert() is used to INSERT record into collection

Check the output below :: 




In our next article, we'll see various ways of querying Mongo. 

Thursday, February 18, 2016

Wamp and MongoDB - II

In our previous article, we have seen installation of MongoDB on our C: drive (Windows Environment) at path "c:\mongodb\" and installed it as a Win32 Service. Now let's try to work with a bit. Let's do some basic Database stuffs.

Open command prompt and set path to the "c:\mongodb\bin" directory as shown below :

path=%path%;c:\mongodb\bin\

%path% refers to "path" environment variable; We are just appending new path to it. 

Next, we type "mongo" to start the Mongo Shell where we can run certain commands to do specific database tasks. See below what happens when we start the Mongo shell : 



MongoDB uses "Collections" which is similar to "Tables" in any R/DBMS. It also uses JSON like name/field value pair and such pairs are called "Documents". Here, JSON field value pairs are in BSON (Binary JSON) format which have additional type information. 

Next, we would try some commands at the prompt. 
   
1. db :: Will show you current Database
2. show dbs :: will show list of Databases
3. use mytestdb :: (without quote), Switch to a Database "mytestdb" even though it may not exist.

Now, we'll try CRUD (Create, Read, Update, Delete) operations. Let's insert some data inside a table called 'students' in 'myDatabase' Database.

1. use myDatabase    
2. db.myDatabase.find()    
3. db.myDatabase.insert({'fname':'Atal','lname':'Singh','roll':1})
4. db.myDatabase.insert({'fname':'Arin','lname':'Sen','roll':2})
5. db.myDatabase.insert({'fname':'Avik','lname':'Roy','roll':3})
6. db.myDatabase.insert({'fname':'Anik','lname':'Paul','roll':4})
7. db.myDatabase.insert({'fname':'Amal','lname':'Sen','roll':5})
8. db.myDatabase.insert({'fname':'Ajay','lname':'Dev','roll':6})
9. db.myDatabase.find()  
10. show collections  

The first command selects our database myDatabase.
The second command issues a SELECT SQL command with no parameter specified. So it returns all the record in the collection 'myDatabase'.
The 3 to 8 commands inserts individual records into that collection.
The 9th command again returns/shows all the records in that collection. 
The 10th command shows all the tables/collections within current selected database

Now we will try to filter the return records, i.e add "WHERE" clause to "SELECT" command. 

Say, we require names of all the students whose last name is 'Sen'. So we need issue a command like this :
db.myDatabase.find({ 'lname':'Sen' })

Here we wanted an exact match of lastname 'Sen'. However for a LIKE type query, we need issue following command.
db.myDatabase.find({ 'lname': /Sen/ })

We just used a REGEXP to specify a LIKE Query.

The find() accepts 2 parameters, first parameter is for filtering (WHERE clause) and second parameter is for selected/returned fields/columns.

Some more SELECT Query examples :: 

1) Select * from myDatabase where roll < 3 ORDER by roll DESC LIMIT 2
   db.myDatabase.find({roll:{$lt:3}}).limit(2).sort({'roll':-1})

  See that,  after the field roll, we used a sub-document { $lt : 3 } where $lt is an operator and it takes 3 as argument. 

   If we need to look for those records where roll < 10 and roll > 5 then we need to write this way: 
   db.myDatabase.find({roll:{$lt:10, $gt:5}})

2) Select fname from myDatabase where lname like '%Sen%' and status = 1
   db.myDatabase.find({'status':1, 'lname':/Sen/}, {'fname':1})
   
3) Select fname, lname from myDatabase where ( status = 0 OR status = 1) AND lname like "%Sen%"
   db.myDatabase.find( 
      { 'lname':/Sen/, 
 $or:[{'status':1}, {'status' : 0}] 
  }, {'fname':1, 'lname':1})

4) Select * from myDatabase where ( roll > 5 and roll <> 8 ) OR lname like "%Sen%"
       db.myDatabase.find(
     {  $or:[ {'lname':/Sen/}, 
      {$and   : [{'roll':{$ne:8}}, {'roll':{$gt:5}}]}
    ]
     },{'_id':0,'roll':1})
   
   db.myDatabase.find(
     { 
     $or:[ {'lname':/Sen/}, {'roll':{$ne:8, $gt:5}}] 
  },{'_id':0,'roll':1})
   
   Any of the above solution would work. 
   Note that '$ne' in MongoDB means '<>' in SQL, '$gt' in MongoDB means '>' in SQL.
   Also '$gte' in MongoDB means '>=' in SQL.

5) Select * from myDatabase order by fname, lname DESC limit 10
   db.myDatabase.find().sort({'fname':1, 'lname':-1}).limit(10);
   
6) Select count(*) from myDatabase
        db.myDatabase.find().count()
  db.myDatabase.count()

Any of the above commands will work.

7) Select distinct(fname) from myDatabase
    db.myDatabase.distinct('fname')

Some UPDATE Query examples :: 
   
1) Update myDatabase SET status = 5 where age > 16
   db.myDatabase.update(
       {age:{$gt:16}}, 
       {$set : {'status':5}}, {multi:true})

2) UPDATE myDatabase set roll = roll + 3 where age < 15   
   db.myDatabase.update(
       {age:{$lt:15}}, 
       {$inc : {'roll':3}}, {multi:true})

Some DELETE Query examples ::    

1) DELETE from myDatabase 
   db.myDatabase.remove()

2) DELETE from myDatabase where roll > 100
   db.myDatabase.remove({roll:{$gt:100}})   
   
3) DELETE from myDatabase where roll > 200 and status = 1
   db.myDatabase.remove({$and:[{roll:{$gt:200}}, {status:1} ]})      
   
In our next article, we'll write PHP code to do the above CRUD operations using MongoDB classes.