Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


PHP and WDDX - Page 4

September 4, 2002

It's interesting to note, also, that wddx_serialize_value() and wddx_serialize_vars() generate significantly different (though valid) WDDX packets. Consider Listing 5.11, which creates a WDDX packet containing the same variable-value pair as Listing 5.3, and compare the resulting output in Listing 5.12 with that in Listing 5.4.

Listing 5.11 Serializing a Single Variable with wddx_serialize_vars()

<?php
$flavor = "strawberry";

print wddx_serialize_vars("flavor");
?>

Listing 5.12 A WDDX Packet Generated via wddx_serialize_vars()

<wddxPacket version='1.0'>
   <header/>
   <data>
      <struct>
         <var name='flavor'>
            <string>strawberry</string>
         </var>
      </struct>
   </data>
</wddxPacket>

The wddx_add_vars() Function

PHP also allows you to build a WDDX packet incrementally, adding variables to it as they become available, with the wddx_add_vars() function. Listing 5.13 demonstrates this approach, building a WDDX packet from the results of a form POST operation.

Listing 5.13 Building a WDDX Packet Incrementally with wddx_add_vars()

<?php

// create a packet handle
// the argument here is an optional comment
$wp = wddx_packet_start
  ("A packet containing a list of form fields with values");

// iterate through POSTed fields
// add variables to packet
wddx_add_vars($wp, "HTTP_POST_VARS");

// end the packet
// you can now assign the generated packet to a variable
// and print it 
wddx_packet_end($wp);
?>

This is a slightly more complicated technique than the ones described previously. Let's go through it step by step:

  1. The first order of business is to create an empty WDDX packet to hold the data; this is accomplished with the aptly named wddx_packet_start() function, which returns a handle for the newly minted packet.

  2. $wp = wddx_packet_start
      ("A packet containing a list of form fields with  _values");

    This handle is used in all subsequent operations. Note that the wddx_packet_start() function can be passed an optional comment string, which is used to add a comment to the header of the generated packet.

  3. With the packet created, the next step is to add data to it. In Listing 5.13, the data is generated dynamically from a form submission, and each value is then added to the packet via the wddx_add_vars() function.

  4. wddx_add_vars($wp, "HTTP_POST_VARS");

    This function works in much the same way as wddx_serialize_vars()—it accepts multiple variable names as argument (although I've only used one here), serializes these variables into WDDX structures, and adds them to the packet. Note, however, that wddx_add_vars() requires, as first argument, the handle representing the packet to which the data is to be added.

  5. After all the required data has been inserted into the packet, the final step is to close the packet, accomplished via the wddx_packet_end() function. Again, the packet handle is used to identify the packet to be closed.

  6. wddx_packet_end($wp);

    Note that the wddx_packet_end() function returns the contents of the newly minted packet; this return value can be assigned to a variable and used in subsequent lines of the PHP script.

    This approach comes in particularly handy if you're dealing with dynamically generated data, either from a database or elsewhere.

With your data now safely encoded into WDDX, let's now look at how you can convert it back into usable PHP data structures.

PHP and WDDX - Page 3
XML and PHP
Decoding Data with WDDX - Page 5


Up to => Home / Authoring / Languages / XML / WebServices / WDDX / XMLandPHP




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers