phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP WDDX: Complex Serialization

Tag: wddx. distributed data, serialization, complex serialization   Category: PHP Application
post: 30 Apr 2008 read: 230


Web Distributed Data Exchange Step By Step tutorial - Part 9: Don't worry about that title, this code is not complex. even, we want to simply serialization complex data. With PHP's function, we will see simple way. We can handle complex data with three functions: wddx_packet_start(), wddx_add_vars(), and wddx_packet_end().

<?php
// example data
$arrBooks = array(array('id'=>1,'title'=>'PHP Undercover'),
                  array('id'=>2,'title'=>'PHP Webservices'),
				  array('id'=>3,'title'=>'Zend Framework')
				  );

// add header
$wddxst = wddx_packet_start('We build a start');

// add some variables
$rowCount   = count($arrBooks);
$fieldCount = 2;

wddx_add_vars($wddxst,'rowCount','fieldCount');

// add complex variable 
$i = 0;
for($i=0;$i<$rowCount;$i++){
  $varname = 'book'.$i;
  $$varname = $arrBooks[$i];

  wddx_add_vars($wddxst, $varname);
}

print wddx_packet_end($wddxst);

?>

The result is like this:


<wddxPacket version='1.0'>
  <header>
    <comment>We build a start</comment>
  </header>
  <data>
    <struct>
      <var name='rowCount'>
        <number>3</number>
      </var>
      <var name='fieldCount'>
        <number>2</number>
      </var>
      <var name='book0'>
        <struct>
          <var name='id'>
            <number>1</number>
          </var>
          <var name='title'>
            <string>PHP Undercover</string>
          </var>
        </struct>
      </var>
      <var name='book1'>
        <struct>
          <var name='id'>
            <number>2</number>
          </var>
          <var name='title'>
            <string>PHP Webservices</string>
          </var>
        </struct>
      </var>
      <var name='book2'>
        <struct>
          <var name='id'>
            <number>3</number>
          </var>
          <var name='title'>
            <string>Zend Framework</string>
          </var>
        </struct>
      </var>
    </struct>
  </data>
</wddxPacket>


Series this article:
PHP WDDX: Introduction
PHP WDDX: Understanding Anatomy of WDDX
PHP WDDX: Understanding Data Elements
PHP WDDX: Understanding Simple Data Type
PHP WDDX: Using Array Elements
PHP WDDX: Struct Element
PHP WDDX: Recordset Element
PHP WDDX: Simple Serialization Using PHP function
PHP WDDX: Complex Serialization
PHP WDDX: Unserializing Data
PHP WDDX: Creating Web Service Server
PHP WDDX: Creating Web Service Client

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting